如何将屏蔽图像保存到相机胶卷或共享社交媒体

时间:2014-05-27 22:21:00

标签: ios objective-c mask

当我试图保存蒙面图像时,我可以完美地掩盖图像完美掩盖图像,它保存为没有蒙版的普通图像。或者有些人可以发布代码以保存蒙版请帮助我。这是代码 -

- (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

1 个答案:

答案 0 :(得分:0)

我不确定你是否还能把它搞清楚但是我遇到了同样的问题。尝试将imageView的图层渲染为以下内容:

 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> 

希望有所帮助!