我有大量借用(标准)代码,它将灰度UIImage蒙版应用于UIImage。
- (void) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef imageRef = image.CGImage; // main UIImage
CGImageRef maskRef = maskImage.CGImage; // grayscale UIImage mask
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imageRef, mask);
maskedImage = [UIImage imageWithCGImage:masked];
CGImageRelease(mask);
CGImageRelease(masked);
imageView.image = maskedImage;
}
然后我将imageView.image保存到文件中。代码就像iOS 6和6.1中的魅力一样;保存的图像包含应用了蒙版的主UIImage。但是,我注意到此代码在iOS 7(iPhone)中的执行方式不同。在iOS 7中,保存的图像仅包含未应用蒙版的主UIImage。我不确定问题是在CGImageCreateWithMask()方法还是其他地方。
任何见解都将受到赞赏。非常感谢你。
答案 0 :(得分:6)
我在我的应用上也发现了这个问题,我通过以下代码解决了这个问题。
- (UIImage*)image:(UIImage*)image withMask:(UIImage*)maskImage
{
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef maskedImageRef = CGImageCreateWithMask([handledImage CGImage], mask);
CGImageRelease(mask);
// Under iOS 7, UIImage with mask no longer applied mask when saving it.
// We draw the image to context and obtain image from context to get the image applied mask
CGContextRef context = CGBitmapContextCreate(nil,
CGImageGetWidth(maskedImageRef),
CGImageGetHeight(maskedImageRef),
CGImageGetBitsPerComponent(maskedImageRef),
CGImageGetBytesPerRow(maskedImageRef),
CGImageGetColorSpace(maskedImageRef),
CGImageGetBitmapInfo(maskedImageRef));
CGRect imageRect = CGRectMake(0, 0, CGImageGetWidth(maskedImageRef), CGImageGetHeight(maskedImageRef));
CGContextDrawImage(context, imageRect, maskedImageRef);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *maskedImage = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);
CGContextRelease(context);
CGImageRelease(maskedImageRef);
return maskedImage;
}
答案 1 :(得分:6)
上面的代码很好地说明了如何处理问题(在上下文中绘图)但不适合我。以下代码适用于IOS 7和以前的版本(IOS 6.x)。
+(UIImage*)maskImageExt:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef imageRef = image.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, true);
CGImageRef maskImageRef = CGImageCreateWithMask([image CGImage], mask);
CGContextRef context = CGBitmapContextCreate(nil,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
CGImageGetBitsPerComponent(imageRef),
CGImageGetBytesPerRow(imageRef),
CGImageGetColorSpace(imageRef),
CGImageGetBitmapInfo(imageRef));
CGRect imageRect = CGRectMake(0, 0, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
CGContextDrawImage(context, imageRect, maskImageRef);
CGImageRef maskedImageRef = CGBitmapContextCreateImage(context);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];
CGImageRelease(mask);
CGContextRelease(context);
CGImageRelease(maskedImageRef);
return maskedImage;
}
答案 2 :(得分:0)
确保两个图像(要遮罩的图像和遮罩本身)都有alpha通道!这解决了我在iOS7上的问题。
然后你应该可以使用你用于iOS6的旧代码。