我从相机获取UIimages并将它们分配给UIImageViews的显示。 当我做这相机给了我一个1200 x 1600的像素的图像,然后我分配给我申请一个UIImageView。 如预期在此条件下的图像视图显示图像。 然而,当我试图将其分配到的UIImageView之前要调整检索到的UIImage,图像调整预期,但存在这样的地方的问题(在RESIZING代码?)我的UIImage越来越ROTATED ......结果,当我分配调整大小的UIImage到一个UIImageView图像被旋转90度和出现拉伸,纵横比(1200 x 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从相机拉不旋转像素?
Answer 1:
您的代码不工作的原因是因为对你的代码的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;}
丹尼尔给出的答案也是正确的,但是从它不是线程安全的问题的困扰,因为你正在使用UIGraphicsBeginImageContext()。 由于上面的代码仅使用CG功能,你都设置。 我也有一个类似的功能调整和做正确的方面补上的图像 - 让我知道如果这就是你要找的内容。
注:我从原来的功能这个帖子 ,并做了一些修改,以使其在JPEG文件的工作。
Answer 2:
我有这个问题太与一些代码在那里,我发现这个代码,工作,检查出来,让我知道你发现了什么
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Answer 3:
斯威夫特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()
}
Answer 4:
如果你想通过双方的宽度或高度裁剪它使从矩形图像(无论是水平或垂直)的正方形图像,用我的代码。 不同的是,我不舒展,但我裁剪。 我从修改前的代码做它:
//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((b-a)/2.0, 0, a, a);
b = a;
}
else if (b<a) {
cropRect = CGRectMake(0, (a-b)/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);
}
文章来源: Resizing UIimages pulled from the Camera also ROTATES the UIimage?