我正在根据用户改变图像的颜色(用户从颜色列表中选择颜色),因为我使用两个图像的叠加混合模式来改变颜色。我有一个字符的第一个图像(img1),它的颜色要改变,我正在从字符图像创建一个图像(resultUIImage),如下所示,并改变其像素值以获得用户选择的颜色。然后我覆盖img1和resultUIImage以获得我的预期结果(img2),但img2是紧张的;它的两个不同颜色细节之间的界限是一条虚线。但是,如果针对ipad测试了相同的项目,则不会出现这些破坏的边界。这里可能出现的错误是什么,还是有其他方法可以做到这一点?
这些图像是png格式,当我尝试使用jpg图像时,没有抖动。但我不能jpg,因为它不支持透明度。
- (UIImage *)getNewImageFromImage:(UIImage *)image {
const int RED = 1, GREEN = 2, BLUE = 3;
int width = image.size.width,
height = image.size.height;
uint32_t * pixels = (uint32_t *) malloc(width*height*sizeof(uint32_t));
memset(pixels, 0, width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [image CGImage]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t * rgbaPixel = (uint8_t *) &pixels[y*width+x];
rgbaPixel[BLUE] = blueclr;
rgbaPixel[GREEN] = greenclr;
rgbaPixel[RED] = redclr;
}
}
CGImageRef newImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);
UIImage * resultUIImage = [UIImage imageWithCGImage:newImage];
CGImageRelease(newImage);
CGSize newSize = CGSizeMake(image.size.width, image.size.height);
UIGraphicsBeginImageContext( newSize );
[ img1.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[ resultUIImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeOverlay alpha:0.8];
img2.image = UIGraphicsGetImageFromCurrentImageContext();
img2.frame=img1.frame;
img2.alpha=1;
UIGraphicsEndImageContext();
return resultUIImage;
}