UIImage混合模式在Retina显示屏上不能很好地工作

时间:2014-01-21 08:29:27

标签: ios objective-c uiimage retina-display

我想要混合一个图像,但有一个问题是我混合后像素几乎失去了一半。我的代码是:

UIImageView *baseIgv2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 76, 76)];
[self.view addSubview:baseIgv2];
[baseIgv2 setImage:[UIImage imageNamed:@"btn_award_open.png"]];
baseIgv2.center = CGPointMake(300, 300);

UIGraphicsBeginImageContext(baseIgv2.bounds.size);

[baseIgv2.image drawInRect:baseIgv2.bounds];
[baseIgv2.image drawInRect:baseIgv2.bounds blendMode:kCGBlendModeScreen alpha:.8];
[baseIgv2.image drawInRect:baseIgv2.bounds blendMode:kCGBlendModeDestinationIn alpha:.8];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[baseIgv2 setImage:newImage];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
anim.beginTime = CACurrentMediaTime();
anim.fromValue = @.5;
anim.toValue= @1;
anim.autoreverses = YES;

anim.duration = .5;
anim.repeatDuration = 1000;
anim.repeatCount = 1000;
[baseIgv2.layer addAnimation:anim forKey:nil];

项目中的图片包含 btn_award_open@2x.png btn_award_open@2x~ipad.png btn_award_open~ipad.png

在我使用混合物之前,它还可以,但在我使用它之后,它不再是视网膜。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

虽然您正在做的是正确的,但您正在使用旧的UIKit函数来创建位图上下文。

要缩放视网膜屏幕的位图上下文,您应该使用此功能:

void UIGraphicsBeginImageContextWithOptions(
   CGSize size,
   BOOL opaque,
   CGFloat scale
);

所以你需要替换这行代码:

UIGraphicsBeginImageContext(baseIgv2.bounds.size);

有了这个:

UIGraphicsBeginImageContextWithOptions(baseIgv2.bounds.size, YES, 0.0);

有关该功能及其参数的更多信息:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html