2011-10-27 12 views
1

我在我的智慧结束自定义属性动画过程中...iOS版:绘制CGImage到的CALayer的子类在正确的分辨率

我有应显示的动画圆截面的视图这是预 - 提供并作为PNG文件(视网膜和非视网膜版本,正确命名; pie_p_000.png与[email protected])。此部分应根据应用程序中某些百分比值的变化进行动画制作。所以我做了一个UIView的子类,它的层次结构中有一个自定义的CALayer。该计划是为CALayer子类实现自定义动画属性,并在重写的方法drawInContext中更改图片。到目前为止这么好,计划的工作和动画显示时,我使用函数setPercentageWithFloat:的视图(下面的完整源代码)更改百分比值。 事情是:我真的不知道为什么我的iPhone4总是呈现低分辨率的图像。我尝试过使用比例因子,但这并没有帮助。无论是图像呈现在正确的大小和低分辨率或图像呈现双倍大小。 覆盖显示:直接设置contents属性会导致动画不显示(在动画期间不显示图像);在动画时间之后显示最终图像。在这种情况下,将呈现正确的分辨率图像。

顺便说一句:下面的代码在错误处理,灵活性和优雅性方面并不十分复杂,但它只是试图让这个东西运行;) - 所以图像仍然呈现翻转等等......

我希望有人对我有暗示。 由于

的视图:

#import "ScrollBarView.h" 
#import "ScrollBarLayer.h" 

@implementation ScrollBarView 

@synthesize sbl; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)setImagesWithName:(NSString*)imageName { 
    ScrollBarLayer *ssbl = [[ScrollBarLayer alloc] init]; 
    ssbl.frame = CGRectMake(0, 0, 30, 30); 
    [ssbl setImagesWithName:imageName]; 
    [self.layer addSublayer:ssbl]; 

    self.sbl = ssbl; 

    [ssbl release]; 
} 

- (void) dealloc { 
    self.sbl = nil; 
    [super dealloc]; 
} 

- (void)setPercentageWithFloat:(CGFloat)perc { 
    if(perc > 1.0){ 
     perc = 1.0; 
    } else if(perc < 0) { 
     perc = 0; 
    } 

    [self.sbl setPercentage:perc]; 

    CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"percentage"]; 
    ba.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    ba.duration = 0.8; 
    ba.toValue = [NSNumber numberWithFloat:perc]; 
    [self.sbl addAnimation:ba forKey:nil]; 
} 
@end 

视图可以被配置成与不同的图像(使用函数setImagesWithName :)使用不同的名称(theName)工作。在此方法中,视图将ScrollBarLayer添加到其图层属性。

图层:

#import "ScrollBarLayer.h" 

@implementation ScrollBarLayer 

@synthesize filename; 
@dynamic percentage; 

- (id)init { 
    self = [super init]; 
    if (self) { 

    } 
    return self; 
} 

- (void)setImagesWithName:(NSString*)imageName { 
    self.frame = CGRectMake(0, 0, 30, 30); 
    self.percentage = 0; 
    self.filename = imageName; 
} 

+ (BOOL) needsDisplayForKey:(NSString*)key { 
    if([key isEqualToString:@"percentage"]) { 
     return YES; 
    } 
    return [super needsDisplayForKey:key]; 
} 

- (void) drawInContext:(CGContextRef)ctx { 

    int imageIdx = (int)roundf((float)199 * self.percentage); 
    NSString *thisfilename = [self.filename stringByAppendingString:[NSString stringWithFormat:@"%03d.png", i+1]]; 
    UIImage* c = [UIImage imageNamed:thisfilename]; 
    CGImageRef img = [c CGImage]; 
    CGSize sz = CGSizeMake(CGImageGetWidth(img), CGImageGetHeight(img)); 

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width, sz.height), NO, 0.0); 
    CGContextDrawImage(ctx, CGRectMake(0, 0, sz.width, sz.height), img); 
    UIGraphicsEndImageContext(); 
} 

- (void) dealloc { 
    self.pictures = nil; 
    self.filename = nil; 
    [super dealloc]; 
} 

@end 

回答

0

我还需要在这个光。我认为你应该检查设备是否有视网膜显示,如果是,你应该替换CGContextDrawImage(...)CGRect(在第二个参数中) )为一半宽度和高度。

#define IS_RETINA_DISPLAY() ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) 

... 
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width, sz.height), NO, 0.0); 

if (IS_RETINA_DISPLAY()) 
{ 
    CGContextDrawImage(ctx, CGRectMake(0, 0, sz.width/2.0f, sz.height/2.0f), img); 
} else 
{ 
    CGContextDrawImage(ctx, CGRectMake(0, 0, sz.width, sz.height), img); 
} 
UIGraphicsEndImageContext(); 
... 

但我不知道这鳃给你一个“平稳”的结果...