3

我正在使用 UIImagePickerController 类从 iPhone 相机拍照。

我正在使用这个委托方法来获取图像。

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

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

}

但是当我在 ImageView 中使用该图像或将图像数据发送到某个 url 时,图像看起来旋转了 90 度。

问题是什么?我做得对吗?

谢谢

4

1 回答 1

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
于 2010-03-04T11:19:06.060 回答