绘制后uiimageview中的ContentMode更改 [英] ContentMode changes in uiimageview after draw

查看:126
本文介绍了绘制后uiimageview中的ContentMode更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在遵循Ray Wenderlics关于如何创建基本绘图应用程序的教程。



除了当我在内容库上绘制内容模式更改时,它已经正常运行了。 uiimageview。



我已将其设置为情节提要中的AspectFill并应用了图像



我认为问题是在我的触摸结束方法中

 覆盖func touchesEnded(_ touches:Set< UITouch> ;,带有事件:UIEvent?){
if!swiped {
//绘制一个点
drawLineFrom(fromPoint:lastPoint,toPoint:lastPoint)
}

//将tempImageView合并到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,hei) ght:mainImageView.frame.size.height),blendMode:CGBlendMode.normal,alpha:不透明度)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

tempImageView.image = nil


}

内容模式显示为scaleToFill或Scale to fit?



有人知道我该如何更改吗?



致谢



托马斯



更新



感谢Matt的指导



我添加了以下代码

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

mainImageView.image?.draw(in:CGRect(x: 0,y:0,宽度:imageSize.width,高度:imageSize.height),blendMode:CGBlendMode.normal,alpha:1.0)
tempImageView.image?.draw(in:CGRect(x:0,y: 0,宽度:imageSize.width,高度t:imageSize.height),blendMode:CGBlendMode.normal,alpha:不透明)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()

我认为我走在正确的轨道上,但是图像现在正在调整尺寸以适合宽高比,而不是宽高比。

解决方案

图像视图的内容模式未更改。 (您只需检查其 contentMode 即可轻松进行测试,该代码在代码中的任何地方都不会发生。)



<问题是你画画的方式!您正在将图像 mainImageView.image 绘制到矩形 CGRect(x:0,y:0,width:mainImageView.frame.size。宽度,高度:mainImageView.frame.size.height)。这会拉伸图像使其完全适合该矩形,从而导致您抱怨的拉伸行为。



换句话说,是说缩放此图像以填充此矩形,这正是发生的情况。


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.

解决方案

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.

这篇关于绘制后uiimageview中的ContentMode更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆