【问题标题】:iOS scale image taken from camera从相机拍摄的 iOS 缩放图像
【发布时间】:2013-11-10 20:29:55
【问题描述】:

我正在尝试编写代码,用户可以从他们的相机拍摄照片,然后它将显示在指定的 UIImageView 中,并上传到服务器以供以后用户使用。我使用 iPad 作为设备。但是,当用户拍照时,它会旋转 90 度。此外,它的比例是错误的。长宽比与拍摄的照片不同。这是我用来缩放和调整大小的代码:http://pastebin.com/HxNkb7Be

将文件上传到我的服务器时,我得到图像的数据如下:

NSData *imageData = UIImageJPEGRepresentation(scaleAndRotateImage(image), 0.90f);

这就是我从相机获取 UIImage 的方式:

// Get the asset representation
ALAssetRepresentation *rep = [asset defaultRepresentation];

// Get the right orientation
UIImageOrientation orientation = UIImageOrientationUp;
NSNumber *orientationValue = [[rep metadata] objectForKey:@"Orientation"];
if (orientationValue != nil) {
    orientation = [orientationValue intValue];
}

// Get the CG image reference and convert to UIImage
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:orientation];

【问题讨论】:

    标签: ios uiimage orientation scaling uiimageorientation


    【解决方案1】:

    可能是因为您使用了 [rep fullResolutionImage] 辅助方法。以下是一个很好的方法,您可以使用它来实现您想要的。 (这也将解决缩放问题)

     - (UIImage *)image:(UIImage *)image scaledCopyOfSize:(CGSize)newSize {
    
     UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
     UIImageOrientation imageOrientation = UIImageOrientationLeft;
    
     switch (deviceOrientation)
     {
     case UIDeviceOrientationPortrait:
     imageOrientation = UIImageOrientationRight;
     NSLog(@"UIImageOrientationRight");
     break;
     case UIDeviceOrientationPortraitUpsideDown:
     imageOrientation = UIImageOrientationLeft;
     NSLog(@"UIImageOrientationLeft");
     break;
     case UIDeviceOrientationLandscapeLeft:
     imageOrientation = UIImageOrientationUp;
     NSLog(@"UIImageOrientationUp");
     break;
     case UIDeviceOrientationLandscapeRight:
     imageOrientation = UIImageOrientationDown;
     NSLog(@"UIImageOrientationDown");
     break;
     default:
     NSLog(@"Default");
     imageOrientation = UIImageOrientationRight ;
     break;
     }
    
     CGImageRef imgRef = image.CGImage;
    
     CGFloat width = CGImageGetWidth(imgRef);
     CGFloat height = CGImageGetHeight(imgRef);
    
     CGAffineTransform transform = CGAffineTransformIdentity;
     CGRect bounds = CGRectMake(0, 0, width, height);
     if (width > newSize.width || height > newSize.height) {
     CGFloat ratio = width/height;
     if (ratio > 1) {
     bounds.size.width = newSize.width;
     bounds.size.height = bounds.size.width / ratio;
     }
     else {
     bounds.size.height = newSize.height;
     bounds.size.width = bounds.size.height * ratio;
     }
     }
    
     CGFloat scaleRatio = bounds.size.width / width;
     CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
     CGFloat boundHeight;
     UIImageOrientation orient = imageOrientation;
     switch(orient) {
    
     case UIImageOrientationUp: //EXIF = 1
     transform = CGAffineTransformIdentity;
     break;
    
     case UIImageOrientationUpMirrored: //EXIF = 2
     transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
     transform = CGAffineTransformScale(transform, -1.0, 1.0);
     break;
    
     case UIImageOrientationDown: //EXIF = 3
     transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
     transform = CGAffineTransformRotate(transform, M_PI);
     break;
    
     case UIImageOrientationDownMirrored: //EXIF = 4
     transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
     transform = CGAffineTransformScale(transform, 1.0, -1.0);
     break;
    
     case UIImageOrientationLeftMirrored: //EXIF = 5
     boundHeight = bounds.size.height;
     bounds.size.height = bounds.size.width;
     bounds.size.width = boundHeight;
     transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
     transform = CGAffineTransformScale(transform, -1.0, 1.0);
     transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
     break;
    
     case UIImageOrientationLeft: //EXIF = 6
     boundHeight = bounds.size.height;
     bounds.size.height = bounds.size.width;
     bounds.size.width = boundHeight;
     transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
     transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
     break;
    
     case UIImageOrientationRightMirrored: //EXIF = 7
     boundHeight = bounds.size.height;
     bounds.size.height = bounds.size.width;
     bounds.size.width = boundHeight;
     transform = CGAffineTransformMakeScale(-1.0, 1.0);
     transform = CGAffineTransformRotate(transform, M_PI / 2.0);
     break;
    
     case UIImageOrientationRight: //EXIF = 8
     boundHeight = bounds.size.height;
     bounds.size.height = bounds.size.width;
     bounds.size.width = boundHeight;
     transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
     transform = CGAffineTransformRotate(transform, M_PI / 2.0);
     break;
    
     default:
     [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
    
     }
    
     UIGraphicsBeginImageContext(bounds.size);
    
     CGContextRef context = UIGraphicsGetCurrentContext();
    
     if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
     CGContextScaleCTM(context, -scaleRatio, scaleRatio);
     CGContextTranslateCTM(context, -height, 0);
     }
     else {
     CGContextScaleCTM(context, scaleRatio, -scaleRatio);
     CGContextTranslateCTM(context, 0, -height);
     }
    
     CGContextConcatCTM(context, transform);
    
     CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
     UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
    
     return imageCopy;
     }
    

    【讨论】:

    • 效果很好。但是,我注释掉了阻止它缩放图像的部分 up。似乎是多余的,因为方法签名的任何内容都暗示它只能按比例缩小。如果您只想缩小CGImageSourceCreateThumbnailAtIndex 可能是更简洁的解决方案。
    猜你喜欢
    • 2011-07-15
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 2016-02-07
    相关资源
    最近更新 更多