2010-01-09 224 views
3

我使用UIImagePickerController類從iPhone相機拍攝照片。使用UIImagePickerController拍攝圖像

我使用這種委託方法獲取圖像。

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

     UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

} 

但是,當我在ImageView中使用該圖像或將圖像數據發送到某個url時,圖像出現旋轉90度。

問題是什麼?我做得對嗎?

謝謝

回答

6

您需要根據自己的方向自己旋轉圖片。

使用此代碼(也可以調整您的圖片),我發現它的地方,中網,但不記得在哪裏:

@implementation UIImage (Resizing) 

static inline double radians (double degrees) {return degrees * M_PI/180;} 


- (UIImage*)imageByScalingToSize:(CGSize)targetSize { 
UIImage* sourceImage = self; 
CGFloat targetWidth = targetSize.width; 
CGFloat targetHeight = targetSize.height; 

CGImageRef imageRef = [sourceImage CGImage]; 
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 

if (bitmapInfo == kCGImageAlphaNone) { 
    bitmapInfo = kCGImageAlphaNoneSkipLast; 
} 

CGContextRef bitmap; 

if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { 
    bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

} else { 
    bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

}  

if (sourceImage.imageOrientation == UIImageOrientationLeft) { 
    CGContextRotateCTM (bitmap, radians(90)); 
    CGContextTranslateCTM (bitmap, 0, -targetHeight); 

} else if (sourceImage.imageOrientation == UIImageOrientationRight) { 
    CGContextRotateCTM (bitmap, radians(-90)); 
    CGContextTranslateCTM (bitmap, -targetWidth, 0); 

} else if (sourceImage.imageOrientation == UIImageOrientationUp) { 
    // NOTHING 
} else if (sourceImage.imageOrientation == UIImageOrientationDown) { 
    CGContextTranslateCTM (bitmap, targetWidth, targetHeight); 
    CGContextRotateCTM (bitmap, radians(-180.)); 
} 

CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef); 
CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
UIImage* newImage = [UIImage imageWithCGImage:ref]; 

//CGContextRelease(bitmap); 
//CGImageRelease(ref); 

return newImage; 
} 

@end 
相關問題