2012-10-19 4 views
1

나는 이것을 몇 시간 동안 애써왔다. 어파 인 변환은 내가 기대 한대로하지 않는다.iOS : CGImage를 회전하고 UIImage로 회전시키는 방법은 무엇입니까?

CGContextSaveGState (ctx); 
float w = self.frame.size.width; 
float h = self.frame.size.height; 
CGContextRotateCTM (ctx, angle); 
CGContextTranslateCTM (ctx, w/2, -h/2); 
CGRect r = CGRectMake (0,0, w, h); 
CGContextDrawImage (ctx, r, image.CGImage); 
CGContextRestoreGState (ctx); 

나는 image라는 UIImage를 가지고 있는데, 나는 ctx를 통해 접근 할 수있는 CGImage를 가지고있다. 중심에서 Ullmage를 비스듬히 회전시키고 CGImage 위에 그립니다.

회전 한 다음 변환하면 UIImage가 잘못된 방식으로 늘어납니다. 내가 번역 한 다음 회전하면 같은 결과가 발생합니다.

이렇게하는 방법에 대한 단서가 있습니까? 고맙습니다.

답변

2

이 코드는 현재 iOS6에서 작동합니다.

이미지 너비가 높이보다 큰지 확인하고 이미지 높이가 시계 방향으로 회전하는지 확인합니다. 그렇지 않으면 기본 동작 당로드됩니다.

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
UIImage *image = myImage; 

if(image.size.width > image.size.height) { 

    UIImage *image = myImage; 
    image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationRight]; 

    [bgImage setImage:image]; 

} else { 

    [bgImage setImage:image]; 

} 

}