문제

I want to blend a image, but have a problem that the pixel almost lose half after I blend it. My code is:

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];

The picture in project contains btn_award_open@2x.png , btn_award_open@2x~ipad.png, btn_award_open~ipad.png

Before I use blend, it just OK,but after I use it, it's no longer retina. Anyone can help?

도움이 되었습니까?

해결책

Although what you are doing is correct, you are using an old UIKit function to create your bitmap context.

To scale your bitmap context for retina screens you should use this function instead:

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

So you need to replace this line of code:

UIGraphicsBeginImageContext(baseIgv2.bounds.size);

With this:

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

More info about the function and it's parameters:

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top