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

发布于 2024-09-28 11:53:37 字数 1311 浏览 5 评论 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];

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

这是我使用的多个掩模和空白的过程:

  1. 拍摄图像并将其掩模到 获取 maskedImage

  2. 列表项获取掩码并稍微调整其大小 更大:更大的掩码

  3. 混合maskedImage和更大的掩码 拥有 maskedStrokeImage 由 biggrMask 抚摸(这是 我发现添加不规则的唯一方法 不规则蒙版上的描边 image)

  4. 掩码 maskedStrokeImage 与 更大的面具以获得我的最终结果 结果。

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

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

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

I've an app with a lot of masked image. For performance sake I ned to generate those masked image on disk and then incorporate them in the app (they'll be uploaded on demand nut not masked on the fly because it is already done).

I'm using this kind of coding

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

and it works perfectly for my intermediate images but not with the final one.

Here is the process of multiple mask and blan I use :

  1. take an image and mask it to
    obtain the maskedImage

  2. List item take the mask and resize it a bit
    bigger : biggerMask

  3. blend maskedImage and biggermask
    to have the maskedStrokedImage
    stroked by biggrMask (this is the
    only way I found to add an irregular
    stroke on the irregular masked
    image)

  4. mask maskedStrokedImage with
    biggerMask to obtain my final
    result.

The problem is : saving the image obtained on step 1 is ok : I've got a JPG and PNG with exactly what I need.

My goal is to save the result of step 4 to disk but the result is an imahe showing some part of the stroke and the rest is white ...

Any Idea why I'm unable to save the step 4 to disk ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2024-10-05 11:53:37

在这里找到解决方案 https://devforums.apple.com/thread/67565?tstart=0< /a>

在透明背景上使用黑色形状的蒙版,并使用以下方法。蒙版图像完美保存到磁盘,并且可以按原样导入到屏幕,它保持透明度......太棒了!

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

Solution found here https://devforums.apple.com/thread/67565?tstart=0

use mask with black shape on transparent background and use the following method. The masked inmage is saved perfectly to disk and can be imported as is to the screen it keeps its transparency ... 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; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文