UIImage с маской / альфа-странностью - PullRequest
1 голос
/ 01 июля 2011

Я испытываю некоторые странности с UIImage.По сути, я:

  • Загрузка изображения из фотоальбома в UIImage.
  • Создание маски, состоящей из черного эллипса над белым.
  • Использование CGImageCreateWithMask для вырезания части исходного UIImage.
  • Отображение полученного UIImage на экране, и оно выглядит правильно.
  • Сохранение того же UIImage в PNG.

Но когда я смотрю на получившийся PNG-файл, альфа меняется на противоположный!

Есть идеи?

Вот код:

// 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);

Любой и всеПомощь будет принята с благодарностью.Спасибо!

...