1

I have been following Ray Wenderlics's tutorial on how to create a basic draw app.

Ive got it working perfectly except for when I draw the content mode changes on the uiimageview.

I've set it to aspectFill in storyboard and applied an image

I think the problem is in my touches ended method

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !swiped {
        // draw a single point
        drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
    }

    // Merge tempImageView into mainImageView
    UIGraphicsBeginImageContext(mainImageView.frame.size)
    mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: mainImageView.frame.size.width, height: mainImageView.frame.size.height), blendMode: CGBlendMode.normal, alpha: 1.0)
    tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: mainImageView.frame.size.width, height: mainImageView.frame.size.height), blendMode: CGBlendMode.normal, alpha: opacity)
    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    tempImageView.image = nil


}

it changes the content mode to what appears to be scaleToFill or Scale to fit?

Does anybody know how I change this?

regards

Thomas

Update

Thanks Matt for your direction

Ive added the following code

  let imageSize = AVMakeRect(aspectRatio: (mainImageView.image?.size)!, insideRect: mainImageView.frame)

    mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: imageSize.width , height: imageSize.height), blendMode: CGBlendMode.normal, alpha: 1.0)
    tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height), blendMode: CGBlendMode.normal, alpha: opacity)
    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()

I think I'm on the right track but the image is now resizing to aspect fit rather then aspect fill.

Tom
  • 2,358
  • 1
  • 15
  • 30

1 Answers1

0

The content mode of the image view is not changing. (You can test that easily, just by examining its contentMode, which doesn't happen anywhere in your code.)

The problem is the way you draw! You are drawing an image, mainImageView.image, into a rect CGRect(x: 0, y: 0, width: mainImageView.frame.size.width, height: mainImageView.frame.size.height). That stretches the image to fit that rect exactly — resulting in the stretching behavior that you're complaining about.

In other words, you are saying "Scale this image to fill this rect", and that's exactly what's happening.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Can you elaborate on the changes required to prevent this stretching? – DoesData Sep 25 '17 at 16:40
  • @DoesData Unclear what you mean. There is no "issue" here; the code is doing exactly what the OP asked it to. If you have a new question, don't poke at it in the comments; ask a real question about _your_ problem. – matt Sep 25 '17 at 17:06
  • I understand the code is doing what he told it to do. I'm asking how do you properly draw on the image so that this stretching does NOT occur. – DoesData Sep 25 '17 at 19:23
  • And I'm asking _you_ to show what _you're_ doing and what _your_ issue is, in _your own_ question, so we can grapple with that. – matt Sep 25 '17 at 19:35
  • My question: https://stackoverflow.com/questions/46412987/swift-draw-on-image-without-changing-its-size – DoesData Sep 25 '17 at 19:42