我有一张在视网膜设备上看起来清晰干净的图像(带有 X 和 2X 变体)。但我想要一个渐变效果。一旦我在上面画了渐变,图像就会失去清晰度,我猜是因为渐变没有放在精确的图像边界上。任何建议如何解决这个问题。我附上了图像(有和没有渐变效果)以及我的渐变代码:
- (UIImage *)tintedWithLinearGradientColors:(NSArray *)colorsArray forImageNamed:(NSString *)iImageName {
// Load image
UIImage *myIconImage = [UIImage imageNamed:iImageName];
// Create gradient
UIColor *colorOne = [colorsArray objectAtIndex:1]; // top color
UIColor *colorTwo = [colorsArray objectAtIndex:0]; // bottom color
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
UIGraphicsBeginImageContext(myIconImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, myIconImage.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGRect rect = CGRectMake(0, 0, myIconImage.size.width, myIconImage.size.height);
// image drawing code
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextDrawImage(context, rect, myIconImage.CGImage);
// draw tint color, preserving alpha values of original image
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextClipToMask(context, rect, myIconImage.CGImage);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, myIconImage.size.height), 0);
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return coloredImage;
}