[英]How to save masked image to camera roll or to share social media
当我尝试保存被遮罩的图像时,我可以完美地遮罩图像,当我试图保存被遮罩的图像时,可以将其保存为不带遮罩的普通图像。 这是代码-
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef imager = [image CGImage];
CGImageRef maskRef = [maskImage CGImage];
CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imager, actualMask);
return [UIImage imageWithCGImage:masked];
}
- (IBAction)mask:(id)sender
{
imageView.image = [self maskImage:imageView.image withMask:[UIImage imageNamed:@"mask"]];
}
- (IBAction)shareorsave:(id)sender {
NSArray *itemsToShare =@[imageView.image];
UIActivityViewController *activityVC =[[UIActivityViewController alloc]initWithActivityItems:itemsToShare applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}
@end
我不确定您是否仍然可以解决问题,但是我也遇到了同样的问题。 尝试渲染imageView
的层folllows:
if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.frame.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然后添加itemsToShare
newImage
。
不要忘记添加QuartzCore.framework
和
#import <QuartzCore/QuartzCore.h>
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.