Вопрос

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