2011-10-27 2 views
1

에서 사용자 지정 속성 애니메이션 동안의 CALayer의 서브 클래스로의 CGImage 그리기 아이폰 OS : ... 내 지혜의 끝에있어 정확한 해상도

내가 애니메이션 원형 단면을 표시해야하는 뷰를 가지고있는 사전이다 - 렌더링 및 png 파일 (망막과 비 망막 버전 모두, 올바르게 명명 된; pie_p_000.png vs. [email protected])으로 사용할 수 있습니다. 이 섹션은 앱 내 일부 백분율 값의 변화에 ​​따라 애니메이션되어야합니다. 그래서 레이어 계층 내에서 사용자 정의 CALayer가있는 UIView의 하위 클래스를 만들었습니다. CALayer 서브 클래스에 대한 사용자 정의 animatable 속성을 구현하고 재정의 된 메서드 drawInContext :의 그림을 변경하는 것이 계획입니다. 지금까지는 그렇게 좋았습니다. 계획의 효과가 있었고 뷰의 함수 setPercentageWithFloat :를 사용하여 백분율 값을 변경할 때 애니메이션이 표시되었습니다 (아래 전체 소스). 문제는 : 내 iPhone4가 항상 저해상도 이미지를 표시하는 이유를 알 수 없습니다. 나는 이미 스케일 요소로 놀아 봤지만 도움이되지 못했습니다. 이미지가 올바른 크기와 저해상도로 표시되거나 이미지가 두 배로 표시됩니다. 표시를 재정의하고 내용 속성을 직접 설정하면 애니메이션이 표시되지 않는 효과가 있습니다 (애니메이션 중에는 이미지가 표시되지 않습니다). 애니메이션 시간이 지나면 최종 이미지가 표시됩니다. 이 경우 올바른 해상도 이미지가 표시됩니다.

Btw : 다음 코드는 오류 처리, 유연성 및 우아함에서 아직 정교하지는 않지만 아직 실행중인 것은 아닙니다.) - 이미지가 여전히 뒤집혀서 표시되는 등 ...

누군가 내게 힌트를 주길 바랍니다. 감사

:보기 다른 이름 (theName)를 사용하여 기능 setImagesWithName :)를 사용하여 다른 이미지 (함께 작동하도록 구성 할 수 있습니다

#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 

. 이 메서드 내에서 View는 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(); 
... 

하지만이 아가미가 당신에게 "부드럽게"결과를 제공 모르겠어요 ...