如何将遮罩对准图像 [英] How to align a mask onto an image

查看:61
本文介绍了如何将遮罩对准图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Swift编写代码,尝试使用UIImage和CALayer屏蔽图像。问题在于,将蒙版应用于图像时会缩放和移动蒙版。我已经使用Interface Builder创建了UIImageViewer,并将其设置为Aspect Fit。我想遮罩任意大小的图像。我将蒙版图像生成为与图像相同的大小。这是一项要求,因为以后我想以编程方式向蒙版添加更多元素,并且必须覆盖蒙版图像。这是我的代码:

I'm writing in Swift, trying to mask an image using UIImage and CALayer. The problem is that the mask is scaled and shifted when applied to the image. I have created my UIImageViewer using the Interface Builder and I have set it to Aspect Fit. I would like to mask images of arbitrary size. I generate the mask image to be the same size as the image. This is a requirement because later I want to add more elements to my mask programmatically and it has to cover the masked image. Here's my code:

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var img = UIImage(named: "flowers.png")

    var maskImageSize = CGSizeMake(img!.size.width, img!.size.height)

    UIGraphicsBeginImageContextWithOptions(maskImageSize, false, 0.0)

    var color = UIColor(white: 1.0, alpha: 0.0)
    color.setFill()
    var rect = CGRectMake(0, 0, img!.size.width, img!.size.height)
    UIRectFill(rect)

    color = UIColor(white: 0.0, alpha: 1.0)
    color.setFill()
    rect = CGRectMake((img!.size.width/2)-50, (img!.size.height/2)-50, 100, 100)
    UIRectFill(rect)

    var maskImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    var maskLayer = CALayer()
    maskLayer.frame = self.imageView.layer.bounds // it'll have the same problem if I set it to self.imageView.layer.frame
    maskLayer.contents = maskImage.CGImage
    maskLayer.contentsGravity = kCAGravityCenter

    self.imageView.image = img
    self.imageView.layer.mask = maskLayer;
    self.imageView.layer.masksToBounds = true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

我需要知道如何将图像遮罩对准原始图像。谢谢您的帮助!

I need to know how to align the image mask onto the original image. Thanks for your help!

我有相当不错的屏幕截图,但是Stackoverflow不允许我发布它们,因为我没有足够的声誉!

I have pretty good screen shots but Stackoverflow doesn't let me to post them because I don't have enough reputation!

推荐答案

此处的问题是,如果使用宽高比,则显示的图像大小取决于原始图像的大小和大小图像视图。您试图做的是模仿图像视图对图像的作用-并且您无法确切知道图像的含义。

The problem here is that if you use aspect fit then the size of the image as displayed depends upon both the size of the original image and the size of the image view. What you are trying to do is to imitate what the image view does to the image - and you have no way of knowing exactly what that is.

您可以做的最好的事情是猜测。您要寻找的是最大的矩形大小,该大小将适合图像视图的边界,同时保持原始图像的长宽比。碰巧,如果导入AV Foundation框架,则会有一个方便的函数为您提供该信息 AVMakeRectWithAspectRatioInsideRect (在此处记录: https://developer.apple.com/library/ios/文档/AVFoundation/Reference/AVFoundation_Functions/index.html#//apple_ref/c/func/AVMakeRectWithAspectRatioInsideRect )。

The best you can do is guess. What you are looking for is the size of the largest rectangle that will fit into your image view's bounds while keeping the aspect ratio of the original image. It happens that if you import the AV Foundation framework, there's a handy function that gives you that information, AVMakeRectWithAspectRatioInsideRect (documented here: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVFoundation_Functions/index.html#//apple_ref/c/func/AVMakeRectWithAspectRatioInsideRect).

这篇关于如何将遮罩对准图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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