我拍摄了一张带有 alpha 的白色图像并为其着色。这与 iOS 6 中的以下代码完美配合。但是,在 iOS 7 中,使用此代码后,非零 alpha 图像的边缘周围仍保留一个像素宽的白色边框。两个操作系统之间有什么变化可以保持像素宽的白色边框?
-(void)changeImageColorToColor: (UIColor *)color
CGFloat redC = 0.0, greenC = 0.0, blueC = 0.0;
const CGFloat *components = CGColorGetComponents(color.CGColor);
redC = components[0];
greenC = components[1];
blueC = components[2];
CGImageRef sourceImage = self.addedImageView.whiteColorVersion;
CFDataRef theData;
theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));
CFMutableDataRef mutableData = CFDataCreateMutableCopy(0, 0, theData);
UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(mutableData);
int dataLength = CFDataGetLength(mutableData);
for (int index = 0; index < dataLength; index += 4) {
pixelData[index + 0] = pixelData[index + 0] * redC;
pixelData[index + 1] = pixelData[index + 1] * greenC;
pixelData[index + 2] = pixelData[index + 2] * blueC;
}
CGContextRef context;
context = CGBitmapContextCreate(pixelData,
CGImageGetWidth(sourceImage),
CGImageGetHeight(sourceImage),
8,
CGImageGetBytesPerRow(sourceImage),
CGImageGetColorSpace(sourceImage),
kCGImageAlphaPremultipliedLast);
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newCGImage];
CGContextRelease(context);
CFRelease(theData);
CFRelease(mutableData);
CGImageRelease(newCGImage);
self.addedImageView.image = newImage;
}