2011-11-08 5 views
0

이것은 큰 프로젝트의 일부이지만 문제는 다음과 같이 단순화 할 수 있습니다. ImageView와 "회전"버튼이있는 간단한보기가 있습니다. 버튼을 누를 때마다 ImageView 내부의 이미지가 90도 회전합니다. I에 유래하고 다른 사이트에서 발견 한 내용의 많은 부분에서이 작동합니다 (내 이미지는 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 인 경우 rotationView 이미지의 크기가 올바른지 (예 : 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가 변환 된 이미지를 제공하지 않겠습니다. –

관련 문제