gpt4 book ai didi

从相机拍摄的 iOS 缩放图像

转载 作者:行者123 更新时间:2023-11-28 19:06:45 24 4
gpt4 key购买 nike

我正在尝试编写代码,让用户可以从他们的相机拍摄照片,然后它会显示在指定的 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;
}

关于从相机拍摄的 iOS 缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19696272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com