简体   繁体   English

iOS Image周围的白色像素使用CIFilter

[英]White pixels around iOS Image using CIFilter

I add a picture frame (Image with transparent background) around an existing UIImage and save it all as one image. 我在现有的UIImage周围添加了一个相框(带透明背景的图像),并将其全部保存为一个图像。 On simulator, everything looks like it runs great. 在模拟器上,一切看起来都很棒。 However on the device, it adds some white pixels around some of the areas of the frame's image. 但是在设备上,它会在帧图像的某些区域周围添加一些白色像素。 Here is my code: 这是我的代码:

- (void)applyFilter {
    NSLog(@"Running");

    UIImage *borderImage = [UIImage imageNamed:@"IMG_8055.PNG"];

    NSData *dataFromImage = UIImageJPEGRepresentation(self.imgView.image, 1);

    CIImage *beginImage= [CIImage imageWithData:dataFromImage];

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *border =[CIImage imageWithData:UIImagePNGRepresentation(borderImage)];
    border = [border imageByApplyingTransform:CGAffineTransformMakeScale(beginImage.extent.size.width/border.extent.size.width, beginImage.extent.size.height/border.extent.size.height)];

    CIFilter *filter= [CIFilter filterWithName:@"CISourceOverCompositing"];  //@"CISoftLightBlendMode"];
    [filter setDefaults];
    [filter setValue:border forKey:@"inputImage"];

    [filter setValue:beginImage forKey:@"inputBackgroundImage"];

    CIImage *outputImage = [filter valueForKey:@"outputImage"];







    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *newImg = [UIImage imageWithCGImage:cgimg];

    self.imgView.image = newImg;

}

Here is the resulting image: 这是结果图像:

在此输入图像描述

The frame image used in the picture looks like this: 图片中使用的帧图像如下所示:

在此输入图像描述

Here is a screenshot of the frame image in photoshop, showing those pixels are not present in the PNG. 这是photoshop中帧图像的屏幕截图,显示PNG中不存在这些像素。

在此输入图像描述

The issue is that if you look at your image, those pixels immediately adjacent to the musical notes are apparently not transparent. 问题是,如果你看你的图像,那些紧邻音符的像素显然是不透明的。 And if you notice, those white pixels that appear in the final image aren't just the occasional pixel, but they appear in square blocks. 如果你注意到,那些出现在最终图像中的白色像素不仅仅是偶然的像素,而是出现在方块中。

These sorts of squared-off pixel noise is a telltale sign of JPEG artifacts. 这些平方像素噪声是JPEG伪像的标志。 It's hard to say what's causing this because the image you added to this question was a JPEG (which doesn't support transparency). 很难说是什么导致了这个问题,因为您在此问题中添加的图像是JPEG(不支持透明度)。 I assume you must have a PNG version of this backdrop? 我想你必须有这个背景的PNG版本? You might have to share that with us to confirm this diagnosis. 您可能需要与我们分享以确认此诊断。

But the bottom line is that you need to carefully examine the original image and the transparency of those pixels that appear to be white noise. 但最重要的是,您需要仔细检查原始图像和那些看似白噪声的像素的透明度。 Make sure that as you create/manipulate these images, avoid JPEG file formats, because it loses transparency information and introduces artifacts. 确保在创建/操作这些图像时避免使用JPEG文件格式,因为它会丢失透明度信息并引入工件。 PNG files are often safer. PNG文件通常更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM