[英]iOS scale image taken from camera
我正在嘗試編寫代碼,以便用戶可以從他們的相機拍攝照片,然后它將顯示在指定的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];
可能是由於您使用了輔助方法[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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.