将屏蔽的UIImage保存到磁盘会产生错误的图像

时间:2010-10-17 15:13:33

标签: iphone uiimage mask stroke

我有一个带有大量蒙版图像的应用。为了性能起见,我需要在磁盘上生成这些蒙版图像,然后将它们合并到应用程序中(它们将根据需要上传,因为它已经完成,因此无需动态屏蔽)。

我正在使用这种编码

NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Test_%d.png",i]];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Test_%d.jpg",i]];

// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation([self maskImageWithStroke:image withMask:maskImage], 1.0) writeToFile:jpgPath atomically:YES];

// Write image to PNG
[UIImagePNGRepresentation([self maskImageWithStroke:image withMask:maskImage]) writeToFile:pngPath atomically:YES];

它适用于我的中间图像,但不适用于最终图像。

以下是我使用多个蒙版和blan的过程:

  1. 拍摄图像并将其遮盖 获取maskedImage

  2. 列表项取屏,稍稍调整一下 更大:更大的任务

  3. 混合maskedImage和greatermask 拥有maskedStrokedImage 由biggrMask抚摸(这是 我发现添加不规则的唯一方法 中风不规则蒙面 图像)

  4. 使用屏蔽掩码maskedStrokedImage 更大的获得我的决赛 结果

  5. 问题是:保存在步骤1中获得的图像是可以的:我有一个JPG和PNG正是我需要的。

    我的目标是将步骤4的结果保存到磁盘,但结果是显示笔划的某些部分,其余部分是白色......

    任何想法为什么我无法将第4步保存到磁盘?

1 个答案:

答案 0 :(得分:3)

此处找到解决方案https://devforums.apple.com/thread/67565?tstart=0

在透明背景上使用黑色形状的遮罩,并使用以下方法。蒙面的图像完美地保存到磁盘上,可以按原样导入到屏幕上,保持透明度...... grooovy !!!

-(UIImage*)maskImage:(UIImage *)givenImage withClippingMask:(UIImage *)maskImage 
{
 UIGraphicsBeginImageContext(givenImage.size);
 CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
 CGContextTranslateCTM(currentContext, 0, givenImage.size.height);
 CGContextScaleCTM(currentContext, 1.0, -1.0);
 CGRect rectSize = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height);
 CGContextClipToMask(currentContext,rectSize, maskImage.CGImage);
 CGContextDrawImage(currentContext, rectSize, givenImage.CGImage); 
 UIImage *imgMasked = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return imgMasked; 
}
相关问题