2012-07-29 2 views
6

카메라 롤에서 이미지를로드하지만 이미지의 위아래가 뒤집습니다. 그래서 나는 그것을 회전시키는 방법을 썼다.이미지를 거꾸로 회전하지만 수평으로 뒤집는 방법

CGImageRef imageRef = [image CGImage]; 

float width = CGImageGetWidth(imageRef); 
float height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
Byte *rawData = malloc(height * width * 4); 
Byte bytesPerPixel = 4; 
int bytesPerRow = bytesPerPixel * width; 
Byte bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 
int byteIndex = 0; 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 

Byte *rawData2 = malloc(height * width * 4); 

for (int i = 0 ; i < width * height ; i++) { 
    int index = (width * height) * 4; 
    rawData2[byteIndex + 0] = rawData[index - byteIndex + 0]; 
    rawData2[byteIndex + 1] = rawData[index - byteIndex + 1]; 
    rawData2[byteIndex + 2] = rawData[index - byteIndex + 2]; 
    rawData2[byteIndex + 3] = rawData[index - byteIndex + 3]; 

    byteIndex += 4; 
} 

CGContextRef ctx = CGBitmapContextCreate(rawData2, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef), 8, CGImageGetBytesPerRow(imageRef), CGImageGetColorSpace(imageRef),kCGImageAlphaPremultipliedLast); 

imageRef = CGBitmapContextCreateImage (ctx); 
image = [UIImage imageWithCGImage:imageRef]; 

CGContextRelease(context); 

return image; 

괜찮아요,하지만 지금은 수평으로 뒤집어 야합니다. 어떻게해야 할 지 모르겠습니다. 나는이 두 번째 날을하려고 노력한다.

imageView.transform = CGAffineTransformMakeScale(-1, 1); 

:

은이를 시도

답변

21

도움을 주셔서 감사합니다?

imageView.transform = CGAffineTransformMakeRotation(M_PI); 

당신은 이런 일에 두 가지 변화를 CONCAT 수 있습니다 :

또한 변환을 사용하여 회전을 할 수

imageView.transform = CGAffineTransformRotation(CGAffineTransformMakeScale(-1, 1), M_PI); 

당신이 당신의 자신의 UIImage 개체를 확인하려면 보기 및 변환을 조작하는 대신 위에 설명 된 방법을 사용하여보기가 원하는대로 이미지를 그려 넣은 다음 UIView 내용을 UIImage 객체로 변환하는 것이 좋습니다.

UIGraphicsBeginImageContext(rect.size); 
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

나는 UIImage를 변형하고 CGContextRef에 그려야하므로'.transform'을 사용할 수 없습니다. 나는 UIImageView에 UIImage를 추가하고 회전시키고'image = imageView.image'를 만들려고 시도하지만 작동하지 않습니다. –

+0

보기 내용을 이미지로 어떻게 "변환"할 수 있는지 편집하십시오. – sergio

+0

작동하고 있지만'imageview.frame = CGRectMake (111,122,768,1024)'를 설정하고 컨텍스트를 '0,0,768,1024'로 렌더링합니다. 어떠한 제안? –

관련 문제