0

我有一个带有很多蒙版图像的应用程序。出于性能考虑,我需要在磁盘上生成这些蒙版图像,然后将它们合并到应用程序中(它们将按需上传,而不是动态蒙版,因为它已经完成了)。

我正在使用这种编码

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];

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

下面是我使用的多个 mask 和 blan 的过程:

  1. 拍摄图像并对其进行遮罩以获得 maskedImage

  2. 列表项获取掩码并将其调整为更大一点:bigMask

  3. 混合 maskedImage 和 largemask 以让 biggrMask 描边 maskedStrokedImage (这是我发现在不规则蒙版图像上添加不规则描边的唯一方法)

  4. 用更大的Mask 遮罩 maskedStrokedImage 以获得我的最终结果。

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

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

知道为什么我无法将步骤 4 保存到磁盘吗?

4

1 回答 1

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; 
}
于 2010-10-19T07:36:23.377 回答