我正在经历UIImage的一些奇怪。基本上,我是:
但是当我查看生成的PNG文件时,alpha会反转!
有任何想法吗?
以下是代码:
// originalImage is the UIImage loaded from the photo album
CGImageRef cgImage = [originalImage CGImage];
// make sure that this has an alpha channel
CGImageRef shrunk1 = CopyImageAndAddAlphaChannel(shrunk);
// create oval mask
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef oc = CGBitmapContextCreate(NULL, desiredWidth, desiredHeight,
8,
desiredWidth * 4,
colorspace,
kCGImageAlphaNoneSkipLast);
// over white bgd
CGContextSetRGBFillColor(oc, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(oc, CGRectMake(0, 0, desiredWidth, desiredHeight));
// black oval
CGContextSetRGBFillColor(oc, 0.0, 0.0, 0.0, 1.0);
CGContextFillEllipseInRect(oc, CGRectMake(0, 0, desiredWidth, desiredHeight));
CGImageRef mask1 = CGBitmapContextCreateImage(oc);
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(mask1), CGImageGetHeight(mask1), CGImageGetBitsPerComponent(mask1), CGImageGetBitsPerPixel(mask1), CGImageGetBytesPerRow(mask1), CGImageGetDataProvider(mask1), NULL, NULL);
// cut out the original image with the oval mask
CGImageRef shrunk2 = CGImageCreateWithMask(shrunk1, mask);
// save it
UIImage *final = [UIImage imageWithCGImage:shrunk2];
NSString *s = [self getDateTimeFilename];
NSString *pngPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@.png", s];
[UIImagePNGRepresentation(final) writeToFile:pngPath atomically:YES];
CGImageRelease(shrunk2);
CGImageRelease(mask);
CGImageRelease(mask1);
CGContextRelease(oc);
CGColorSpaceRelease(colorspace);
非常感谢任何和所有帮助。谢谢!