2010-04-18 2 views
1

최근보기의 drawRect 메서드에서 경로를 통해 이미지 자르기에 관한 질문을했습니다.경로가있는 이미지의 다른 부분을 자르기

iPhone clip image with path

은 Krasnyk의 코드는 아래에 복사됩니다.

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGMutablePathRef path = CGPathCreateMutable(); 
//or for e.g. CGPathAddRect(path, NULL, CGRectInset([self bounds], 10, 20)); 
    CGPathAddEllipseInRect(path, NULL, [self bounds]); 
    CGContextAddPath(context, path); 
    CGContextClip(context); 
    CGPathRelease(path); 
    [[UIImage imageNamed:@"GC.png"] drawInRect:[self bounds]]; 
} 

아주 잘 작동합니다. 그러나 내 이미지가보기 자체보다 클 때 이미지의 다른 부분을 어떻게 표시합니까?

타원 및/또는 UIImage drawInRect의 위치 (위의 경계로 표시)에서 번역을 시도해 보았지만 설명 할 수없는 복잡한 효과 (원하지 않는 클리핑, 별난 엘립 크기)가 발생했습니다.


편집 : 어쩌면 내가 내 자신의 질문에 대답 할 수 있습니다. drawInRect 대신 drawAtPoint를 사용할 수 있습니까? 또는 drawInRect를 사용하고 원점을 다른 위치로 설정하지만 사각형의 크기를 동시에 확장합니까?

뷰를 통해 보이는 것보다 큰 직사각형을 그릴 때 성능 저하가 발생합니까?

답변

1

당신이 직접 알아 낸 것 같은 소리. drawAtPoint를 사용해야합니다. drawInRect는 이미지를 대상 rect에 맞도록 크기를 조정합니다. 이는 계산적으로 더 비쌉니다. 이미지가 뷰보다 크다고 가정하면 이미지의 내부 부분을 잘라내 기 위해 음수 x 및 y 값을 drawAtPoint로 전달하게됩니다.

은 예컨대 아래 도면에서, 중앙부에게 화상을 표시한다 :

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddEllipseInRect(path, NULL, [self bounds]); 
    CGContextAddPath(context, path); 
    CGContextClip(context); 
    CGPathRelease(path); 
    UIImage *bigImage = [UIImage imageNamed:@"GC.png"]; 
    CGPoint topLeftOffset = CGPointMake((self.bounds.size.width - bigImage.size.width)/2,(self.bounds.size.height - bigImage.size.height)/2); 
    [bigImage drawAtPoint: topLeftOffset]; 

}

관련 문제