在Swift 2.0中屏蔽图像

时间:2015-09-04 17:58:46

标签: swift swift2 uiimage ios9 core-graphics

我到处搜索并找到了一些关于如何屏蔽图像的例子,但它们似乎都不适合我...我在iOS9上运行并使用Swift2.0

现在这就是我所拥有的:

class func maskImage(background: UIImage, withMask mask: UIImage) -> UIImage {

    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue
    let colorSpace = CGColorSpaceCreateDeviceRGB()!
    let context = CGBitmapContextCreate(nil, CGImageGetWidth(mask.CGImage), CGImageGetHeight(mask.CGImage), 8, 0, colorSpace, bitmapInfo)

    CGContextDrawImage(context, CGRectMake(0, 0, mask.size.width * mask.scale, mask.size.height * mask.scale), mask.CGImage)

    let maskRef: CGImageRef = CGBitmapContextCreateImage(context)!
    let masked: CGImageRef = CGImageCreateWithMask(background.CGImage, maskRef)!

    let icon: UIImage = UIImage(CGImage: masked, scale: mask.scale, orientation: mask.imageOrientation)

    return icon
}

我正在传递两个png并收到错误:我初始化蒙版的行上的EXC_BAD_INSTRUCTION。我没有足够的代表发布图片。

我可能错过了一些超级简单的东西,但会很乐意帮助你。谢谢!

3 个答案:

答案 0 :(得分:1)

使用Swift 3.0,我们已经改名了.... 所以这似乎在一些重命名...和清洁之后起作用... 基于这个例子:(https://www.innofied.com/implementing-image-masking-in-ios/

       func maskImage(image:UIImage, mask:UIImage )->UIImage{

            let imageReference = (image.cgImage)!
            let maskReference = (mask.cgImage)!

            let imageMask = CGImage.init(
                maskWidth: maskReference.width
                ,height: maskReference.height
                ,bitsPerComponent: maskReference.bitsPerComponent
                ,bitsPerPixel: maskReference.bitsPerPixel
                ,bytesPerRow: maskReference.bytesPerRow
                ,provider: maskReference.dataProvider!
                ,decode: nil
                ,shouldInterpolate: true
            )


            return (UIImage(cgImage:(imageReference.masking(imageMask!))!))
        }

答案 1 :(得分:0)

我也在运行最新版本,并且遇到了很多麻烦。我终于能够使用下面的代码裁剪图像的一部分并用图像蒙版覆盖拼图。

import UIKit
import CoreGraphics

class ViewController2: UIViewController {

@IBOutlet var imageView: UIImageView!

var p1PosX: CGFloat = 0.0
var p1PosY: CGFloat = 0.0
var p1Width: CGFloat = 0.0
var p1Height: CGFloat = 0.0

override func viewDidLoad() {
    super.viewDidLoad()

    let panda = UIImage(named: "panda.jpeg")!
    let puzzle1 = UIImage(named: "PP-4-1.gif")!

    //Crop
    let contextSize: CGSize = panda.size
    let rect = CGRect(x: 0, y: 0, width: 200, height: 200)

    p1PosX = 0
    p1PosY = 0
    p1Width = contextSize.width / 2
    p1Height = contextSize.height / 2
    let rect1: CGRect = CGRectMake(p1PosX, p1PosY, 350, 350)
    let imageRef1: CGImageRef = CGImageCreateWithImageInRect(panda.CGImage, rect1)!
    let panda1: UIImage = UIImage(CGImage: imageRef1, scale: panda.scale, orientation: panda.imageOrientation)
    imageView.image = panda1

    //Mask
    UIGraphicsBeginImageContext(imageView.frame.size)
    panda1.drawInRect(rect)
    puzzle1.drawInRect(CGRectMake(0, 0, imageView.frame.size.width , imageView.frame.size.height), blendMode: CGBlendMode.DestinationIn, alpha: 1.0)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    imageView.image = newImage
   } 
}

答案 2 :(得分:0)

如果有人遇到同样的情况并且正在寻找一个干净的解决方案,这里有掩盖图像的功能。它适用于.jpg,.png和.gif图像。

    func maskImage(image:UIImage, mask:(UIImage))->UIImage{

    let imageReference = image.CGImage
    let maskReference = mask.CGImage

    let imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
                                      CGImageGetHeight(maskReference),
                                      CGImageGetBitsPerComponent(maskReference),
                                      CGImageGetBitsPerPixel(maskReference),
                                      CGImageGetBytesPerRow(maskReference),
                                      CGImageGetDataProvider(maskReference), nil, true)

    let maskedReference = CGImageCreateWithMask(imageReference, imageMask)

    let maskedImage = UIImage(CGImage:maskedReference!)

    return maskedImage
}

根据需要在viewDidLoad()或init()中添加以下声明行:

let image = UIImage(named: "image.png")
let maskingImage = UIImage(named: "mask.png")
let imageView = UIImageView(image: maskingImage)
imageView.image = maskImage(image!, mask: maskingImage!)

感谢Manish Kumar的解决方案Implementing Image Masking in iOS