2013-01-10 87 views
0

我有一张图像(带有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; 
} 

enter image description here

enter image description here

回答

0

更换线

UIGraphicsBeginImageContext(myIconImage.size); 
with 
UIGraphicsBeginImageContextWithOptions(myIconImage.size, NO, myIconImage.scale); 

为我工作。

0

您需要通过代码来添加这个梯度?如果不这样做,请在Photoshop或其他软件上添加渐变,然后使用渐变将图像添加到项目中。