2011-11-08 11 views
0

これは大きなプロジェクトの一部ですが、私の問題は次のように簡略化できます: ImageViewと「回転」ボタンを使った簡単なビューがあります。ボタンを押すたびに、ImageView内の画像が90度回転します。私はStackOverflowの他のサイトで見つけたものの多くからは、これは動作するはずです(私のイメージの幅と464に等しい高さを持つ正方形の画像であることに注意してください):回転UIImage:正常に動作しません

- (UIImage *) getRotatedImageFromImage:(UIImage*)image:(int)rotate_index 
{ 
    UIImage *rotatedImage; 

    // Get image width, height of the bounding rectangle 
    CGRect boundingRect = CGRectMake(0, 0, image.size.width, image.size.height); 
    NSLog(@"width = %f, height = %f",boundingRect.size.width,boundingRect.size.height); 
    NSLog(@"rotate index = %d",rotate_index); 

    // Create a graphics context the size of the bounding rectangle 
    UIGraphicsBeginImageContext(boundingRect.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextRotateCTM(context, rotate_index*M_PI/2); 

    // Draw the image into the context 

    [image drawInRect:boundingRect]; 

    // Get an image from the context 
    rotatedImage = UIGraphicsGetImageFromCurrentImageContext(); 

    // Clean up 
    UIGraphicsEndImageContext(); 

    NSLog(@"rotatedImage size = (%f, %f)",rotatedImage.size.width,rotatedImage.size.height); 

    return rotatedImage; 
} 

- (IBAction) rotateImage 
{ 
    NSLog(@"rotate image"); 
    rotateIndex++; 
    if (rotateIndex >= 4) 
     rotateIndex = 0; 
    [imageView setImage: [self getRotatedImageFromImage:imageToSubmit :rotateIndex]]; 
} 

しかし、それはしていません何らかの理由で働く。私が持っているもの:ボタンを押すと、画像はrotateIndexが0になったときにのみ表示され、画像は元の画像と同じです(予想されます)。 rotateIndexが1、2、3の場合、rotateImageの印刷サイズが正しい(つまり、464 x 464)場合でも、imageViewは何も表示しません。 誰かが間違っていることを教えてもらえますか?おかげさまで

答えて

0

私は私の問題を解決することができました。 [:スケール:オリエンテーション:UIImage imageWithCGImage]

CGImageRef cgImageRef = CGBitmapContextCreateImage(context); 
UIImageOrientation imageOrientation; 
switch (rotate_index) { 
    case 0: imageOrientation = UIImageOrientationUp; break; 
    case 1: imageOrientation = UIImageOrientationLeft; break; 
    //2 more cases for rotate_index = 2 and 3 
} 
rotatedImage = [UIImage imageWithCGImage:cgImageRef scale:1.0 orientation:imageOrientation];` 
1
//create rect 
UIImageView *myImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image.png"]]; 

    //set point of rotation 
myImageView.center = CGPointMake(100.0,100.0); 

    //rotate rect 
myImageView.transform =CGAffineTransformMakeRotation(3.14159265/2); //rotation in radians 
+0

私はUIImageViewを使用しようでしたが、変換後、myImageView.imageにアクセスするだけであなたの元画像ではなくなりますここに私のソリューションは、CGImageRefを使用して、あります回転された画像。 UIImageViewから変換されたイメージを取得する方法はありますか? –

+0

あなたはここでプレーする必要があります:(3.14159265/2); 2の代わりに他の値を試してみて、画像変換の変化に気づくでしょう。 – iscavengers

+0

ImageViewで変換された画像に注目します。しかし、私は変換されたイメージをサーバーに送信し、myImageView.imageは私に変換されたイメージを与えません。 –

関連する問題