2012-07-29 20 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); 

あなたはこのようなもので2つの変換をCONCATすることができます:あなたがあなた自身のUIImageオブジェクトを作成したい場合は

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

、ビューと変換を操作するのではなく、上記の方法を使用してビューにイメージを描画し、次に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'に描画します。なにか提案を? –

関連する問題