2010-10-06 62 views
0

我的代碼如下。有誰知道發生了什麼?旋轉UIImage的問題

對不完整的信息。圖像就消失了!

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    UIImage *imCaptured = [info valueForKey:@"UIImagePickerControllerOriginalImage"]; 
    CGImageRef cgirCropped = CGImageCreateWithImageInRect(imCaptured.CGImage, CGRectMake(700.0f, 430.0f, 650.0f, 650.0f)); 
    UIImage *imCropped = [UIImage imageWithCGImage:cgirCropped]; 

    UIGraphicsBeginImageContext(imCropped.size); 
    CGContextRef cgcrRotation = UIGraphicsGetCurrentContext(); 

    switch (imCaptured.imageOrientation) { 
     case UIImageOrientationUp: 
      CGContextRotateCTM(cgcrRotation, (180 * M_PI/180)); 
      break; 
     case UIImageOrientationDown: 

      break; 
     case UIImageOrientationLeft: 

      break; 
     case UIImageOrientationRight: 
      CGContextRotateCTM(cgcrRotation, (90 * M_PI/180)); 
      break; 
    } 
    [imCropped drawAtPoint:CGPointMake(0.0f, 0.0f)]; 
    [imvPreview setImage:UIGraphicsGetImageFromCurrentImageContext()]; 
} 
+0

它幫助,如果你添加了什麼錯誤(錯誤?旋轉錯誤?什麼都不做?) – cobbal 2010-10-06 20:34:13

回答

3

我沒有測試的代碼,但我認爲它圍繞原點旋轉,所以你需要翻譯爲好,這樣的:

... 
case UIImageOrientationUp: 
     CGContextRotateCTM(cgcrRotation, (180 * M_PI/180)); 
     CGContextTranslateCTM(ctxt, -imCropped.size.width, -imCropped.size.height); 
     break; 
...