2012-12-19 3 views
1

버튼을 눌러 UIImageView를 회전하고 싶습니다.각도로 UIImageView 회전하기

@interface UIImage (CS_Extensions) 
- (UIImage *)imageRotatedByRadians:(CGFloat)radians; 
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees; 

을하고하는 .m에서 :

#import "UIImage-Extensions.h" 
UIImage *rotatedImage = [SplashItGroupPicker.image imageRotatedByDegrees:360/arrayCount]; 
SplashItGroupPicker.image = rotatedImage; 

하고 "있는 UIImage-Extensions.h"에

내가 가진 :

- (UIImage *)imageRotatedByRadians:(CGFloat)radians 
{ 
return [self imageRotatedByDegrees:RadiansToDegrees(radians)]; 
} 

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees 
{ 
// calculate the size of the rotated view's containing box for our drawing space 
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width,  self.size.height)]; 
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); 
rotatedViewBox.transform = t; 
CGSize rotatedSize = rotatedViewBox.frame.size; 

// Create the bitmap context 
UIGraphicsBeginImageContext(rotatedSize); 
CGContextRef bitmap = UIGraphicsGetCurrentContext(); 

// Move the origin to the middle of the image so we will rotate and scale around the center. 
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

// // Rotate the image context 
CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); 

// Now, draw the rotated/scaled image into the context 
CGContextScaleCTM(bitmap, 1.0, -1.0); 
CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2, self.size.width, self.size.height), [self CGImage]); 

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return newImage; 

} 

... 회전이 코드를 사용하고 잘 작동하지만 이미지를 회전하면 이미지도 작아지고 이유를 이해할 수 없습니다. 어떤 아이디어?

+0

나는 그것에 대한 연구를하고 싶지 않았기 때문에 대답을 추가하지 않았다. 그러나 내 머리 꼭대기에서 내 생각은 CGContextDrawImage() 코드에있을 것이다. 원산지가 - (크기/2) 인 것처럼 보이지만 크기는 보통 크기입니다. 이미지가 회전 되었기 때문에 크기가 더 이상 일반 크기와 맞지 않습니다. 그래서 원래 크기보다 큰 rect이어야합니다. 회전을 설명합니다. 그 계산이 뭔지 확실하지 않습니다. 그러나 봐야 할 것이 있습니다. –

+0

실제로, 그것은 알레산드로가 인용 한 코드에서 이루어졌습니다. 'rotatedViewBox' 부분을 확인하십시오. – Till

+0

왜 그런 복잡한 방법을 사용하고 새 이미지를 만드나요? CGAffineTransformMakeRotation을 사용하여 이미지보기를 회전시킬 수 없습니까? – rdelmar

답변

2

이것은 망막 스크린에서만 일어나는 것입니까? 그래픽 컨텍스트에서 UIImage를 저장할 때마다 눈금이 정확한지 확인해야합니다. 이것은 내가했던 방법입니다, 나는 사과를 기반으로 망막 장치에서 작동하는 작업 rotateImage 기능을 얻을 수있었습니다 Kekoa의 조각 (감사 톤)를 사용

CGFloat scale = [[UIScreen mainScreen] scale]; 
CGSize size = CGSizeMake(rotatedViewBox.frame.size.width * scale, rotatedViewBox.frame.size.height * scale); 

UIGraphicsBeginImageContext(size); 

/* Do your image manipulations here */ 

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// overwrite the image with one that has the correct scale set 
newImage = [UIImage imageWithCGImage:newImage.CGImage scale:scale orientation:newImage.imageOrientation]; 
+0

망막과 비 망막 – Alessandro

0

을 rotateImage 코드 :

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees { 
    CGFloat radians = DegreesToRadians(degrees); 

    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.size.width, self.size.height)]; 
    CGAffineTransform t = CGAffineTransformMakeRotation(radians); 
    rotatedViewBox.transform = t; 
    CGSize rotatedSize = rotatedViewBox.frame.size; 

    UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]); 
    CGContextRef bitmap = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

    CGContextRotateCTM(bitmap, radians); 

    CGContextScaleCTM(bitmap, 1.0, -1.0); 
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2 , self.size.width, self.size.height), self.CGImage); 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 
0

UIGraphicsGetImageFromCurrentImageContext 할 필요가 없습니다. 메인 스크린의 원하는 부분의 스크린 샷을 얻는 방법. 결과 이미지 품질에 영향을 미칩니다.

[mainImgV setTransform:CGAffineTransformRotate(mainImgV.transform, M_PI)]; 

이 당신의 이미지 뷰를 회전합니다 (당신이 회전보기를 표시 할 경우) 회전 된 이미지를 얻을 수있는 유일한 방법은

처음으로 이미지 뷰를 회전 ...입니다. 지금 당신은이 선으로이 회전 된 이미지를 취할 것입니다. (당신은 또한 회전 imgaeview없이 이러한 라인을 호출 할 수 있습니다.)

CGImageRef cgRef = mainImgV.image.CGImage; 
    newImage = [[UIImage alloc] initWithCGImage:cgRef scale:1.0 orientation:UIImageOrientationDown]; 

저장하거나 스크린 샷을 찍거나 나쁜 품질의 이미지를 손상 할 필요가 없습니다. 그냥 코딩을 즐기십시오.