调整从相机拉取的UIimages的大小也旋转的UIimage?
我从相机获取UIimages并将其分配给UIImageViews以显示。 当我这样做时,相机给我一个1200×1600像素的图像,然后我分配给我的应用程序中的UIImageView。 在此条件下,图像在图像视图中按预期显示。 然而,当我尝试resize的UIImage之前,将其分配给UIImageView,图像resize的预期,但有一个问题,在某处(在RESIZING的代码?)我的UIImage得到旋转…因此,当我resize的UIImage到一个UIImageView图像旋转90度,并出现拉伸的纵横比(1200×1600像素)保持不变…
我正在使用这个从相机获取UIImage:
- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info { myImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; myResizedImg = [self resizeImage:myImg width:400 height:533]; [myImageView setImage:myResizedImg]; }
我正在使用它来调整它的大小:
-(UIImage *)resizeImage:(UIImage *)anImage width:(int)width height:(int)height { CGImageRef imageRef = [anImage CGImage]; CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); if (alphaInfo == kCGImageAlphaNone) alphaInfo = kCGImageAlphaNoneSkipLast; CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo); CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef); CGImageRef ref = CGBitmapContextCreateImage(bitmap); UIImage *result = [UIImage imageWithCGImage:ref]; CGContextRelease(bitmap); CGImageRelease(ref); return result; }
问题:如何在不旋转像素的情况下调整从相机拉出的UIImage?
你的代码不起作用的原因是因为你所拥有的代码的imageOrientation没有被考虑在内。 具体来说,如果imageOrientation是右/左,则需要同时旋转图像和交换宽度/高度。 这是一些代码来做到这一点:
-(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; }
这将调整您的图像,并将其旋转到正确的方向。 如果你需要弧度的定义,它是:
static inline double radians (double degrees) {return degrees * M_PI/180;}
Daniel给出的答案也是正确的,但是由于使用了UIGraphicsBeginImageContext(),所以它遇到了不是线程安全的问题。 由于上面的代码只使用CGfunction,所以你都设置好了。 我也有一个类似的function来resize和做适当的方面填充图像 – 让我知道,如果这是你在找什么。
注:我从这篇文章中得到了原始的function,并做了一些修改,使其在JPEG上工作。
我也有这个问题,有一些代码在那里,我发现这个代码工作,检查出来,让我知道你find
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; { UIGraphicsBeginImageContext( newSize ); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
Swift 3
我把这添加到didFinishPickingMediaWithInfo
方法,然后使用image
而不用担心它的旋转。
var image = info[UIImagePickerControllerOriginalImage] as! UIImage if (image.imageOrientation != .up) { UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale) image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) image = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() }
如果要从矩形图像(无论是水平还是垂直)制作一个正方形图像,请使用我的代码在宽度或高度上进行裁剪。 不同的是我不舒展,但我收获。 我通过修改从顶端的代码:
//Cropping _image to fit it to the square by height or width CGFloat a = _image.size.height; CGFloat b = _image.size.width; CGRect cropRect; if (!(a==b)) { if (a<b) { cropRect = CGRectMake((ba)/2.0, 0, a, a); b = a; } else if (b<a) { cropRect = CGRectMake(0, (ab)/2.0, b, b); a = b; } CGImageRef imageRef = CGImageCreateWithImageInRect([_image CGImage], cropRect); UIImage* sourceImage = _image; CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); CGContextRef bitmap; if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); } else { bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); } if (sourceImage.imageOrientation == UIImageOrientationLeft) { CGContextRotateCTM (bitmap, radians(90)); CGContextTranslateCTM (bitmap, 0, -a); } else if (sourceImage.imageOrientation == UIImageOrientationRight) { CGContextRotateCTM (bitmap, radians(-90)); CGContextTranslateCTM (bitmap, -a, 0); } else if (sourceImage.imageOrientation == UIImageOrientationUp) { // NOTHING } else if (sourceImage.imageOrientation == UIImageOrientationDown) { CGContextTranslateCTM (bitmap, a, a); CGContextRotateCTM (bitmap, radians(-180.)); } CGContextDrawImage(bitmap, CGRectMake(0, 0, a, a), imageRef); CGImageRef ref = CGBitmapContextCreateImage(bitmap); _image = [UIImage imageWithCGImage:ref]; CGImageRelease(imageRef); }