如何清除UIImage的洋红色部分并使其透明?
我已经浏览了很多答案和SO上的链接,没有任何作用(例如,如何在UIImage上使一种颜色透明?答案1删除除红色之外的所有内容,答案2显然不起作用,因为为什么CGImageCreateWithMaskingColors()返回nil in这种情况?).
更新:
如果我使用CGImageCreateWithMaskingColors与UIImage我得到一个零值.如果我删除alpha通道(我将图像表示为JPEG并将其读回),CGImageCreateWithMaskingColors将返回涂有黑色背景的图像.
Update2,代码:
返回零:
const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef imageRef = CGImageCreateWithMaskingColors(anchorWithMask.CGImage, colorMasking);
NSLog(@"image ref %@", imageRef);
// this is going to return a nil imgref.
UIImage *image = [UIImage imageWithCGImage:imageRef];
Run Code Online (Sandbox Code Playgroud)
返回带有黑色背景的图像(这是正常的,因为没有alpha通道):
UIImage *inputImage = [UIImage imageWithData:UIImageJPEGRepresentation(anchorWithMask, 1.0)];
const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef imageRef = CGImageCreateWithMaskingColors(inputImage.CGImage, colorMasking);
NSLog(@"image ref %@", imageRef);
// imgref is NOT nil.
UIImage *image = [UIImage imageWithCGImage:imageRef]; …Run Code Online (Sandbox Code Playgroud) 我有一种方法可以接收UIImage我将其转换为NSData并发出发布该数据的请求,它适用于 iOS 6,但是当我尝试使用 iOS 7 时,图像丢失了透明背景。
这是我迄今为止尝试过的:
-(void)post:(UIImage *)firm name:
{
int x = 350;
NSData *imageData = UIImagePNGRepresentation(firm);
UIImage *image=[UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, x, 40, 50)];
imageView.backgroundColor = [UIColor clearColor];
imageView.image = image;
NSData *imageData2 = [NSData dataWithData:UIImagePNGRepresentation(firm)];
UIImage *image2=[UIImage imageWithData:imageData2];
UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(160, x, 40, 50)];
imageView2.image = image2;
UIImageView *imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(110, x, 40, 50)];
imageView3.image = firm;
UIImage * img = [UIImage imageWithData:UIImagePNGRepresentation(image)];
UIImageView *imageView4 = …Run Code Online (Sandbox Code Playgroud)