调整从相机中提取的 UIimages 的大小也会旋转 UIimage?

Resizing UIimages pulled from the Camera also ROTATES the UIimage?(调整从相机中提取的 UIimages 的大小也会旋转 UIimage?)
本文介绍了调整从相机中提取的 UIimages 的大小也会旋转 UIimage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从相机获取 UIimages 并将它们分配给 UIImageViews 以显示.当我这样做时,相机会给我一个 1200 x 1600 像素的图像,然后我将其分配给我的应用程序中的 UIImageView.在这种情况下,图像在图像视图中按预期显示.但是,当我在将检索到的 UIImage 分配给 UIImageView 之前尝试调整它的大小时,图像正在按预期调整大小,但是在某个地方(在调整大小的代码中?)我的 UIImage 正在旋转...结果,当我将调整大小的 UIImage 分配给 UIImageView 时,图像会旋转 90 度并显示为拉伸,因为纵横比(1200 x 1600 像素)没有改变...

I am getting UIimages from the camera and assigning them to UIImageViews to be displayed. When I do this the camera gives me a 1200 x 1600 pixel image which I then assign to a UIImageView in my Application. The image is displayed as expected in the image view under this condition. However, when I attempt to RESIZE the retrieved UIImage before assigning it to the UIImageView, the image is resizing as expected but there IS a problem in that somewhere (in the RESIZING code?) my UIImage is getting ROTATED... As a result, when I assign the resized UIImage to a UIImageView the image is rotated 90 degrees and appears stretched as the aspect ratio (1200 x 1600 pixels) was unchanged...

我正在使用它从相机中获取 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 是右/左,那么您需要旋转图像并交换宽度/高度.这里有一些代码可以做到这一点:

The reason your code doesn't work is because the imageOrientation on the code that you have is not being taken into account. Specifically, if the imageOrientation is right/left, then you need to both rotate the image and swap width/height. Here is some code to do this:

-(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; 
}

这将调整您的图像大小并将其旋转到正确的方向.如果您需要弧度的定义,则为:

This will resize your image and rotate it to the correct orientation. If you need the definition for radians, it is:

static inline double radians (double degrees) {return degrees * M_PI/180;}

Daniel 给出的答案也是正确的,但它的问题是它不是线程安全的,因为您使用的是 UIGraphicsBeginImageContext().由于上面的代码只使用了 CG 函数,所以你已经准备好了.我也有一个类似的功能来调整图像大小和对图像进行适当的方面填充 - 如果这就是你要找的,请告诉我.

The answer Daniel gave is also correct, but it suffers from the problem that it is not thread-safe, since you're using UIGraphicsBeginImageContext(). Since the above code only uses CG functions, you're all set. I also have a similar function to resize and do proper aspect fill on images - let me know if that's what you're looking for.

注意:我从 这篇文章,并进行了一些修改以使其适用于 JPEG.

Note: I got the original function from this post, and did some modifications to make it work on JPEGs.

这篇关于调整从相机中提取的 UIimages 的大小也会旋转 UIimage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
How to send an image with custom keyboard iOS(如何使用自定义键盘IO发送映像)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)