为了账号安全,请及时绑定邮箱和手机立即绑定

调整从相机拉出的UIimage的大小也会旋转UIimage吗?

调整从相机拉出的UIimage的大小也会旋转UIimage吗?

iOS
小怪兽爱吃肉 2019-12-18 16:17:41
我从相机获取UIimage,并将它们分配给UIImageViews进行显示。当我这样做时,相机会给我一个1200 x 1600像素的图像,然后将其分配给我的应用程序中的UIImageView。在这种情况下,图像将按预期显示在图像视图中。但是,当我尝试在将检索到的UIImage分配给UIImageView之前重新调整其大小时,该图像正在按预期调整大小,但是在某个地方(在RESIZING代码中)存在一个问题,我的UIImage变得旋转了...结果,当我将调整大小的UIImage分配给UIImageView时,图像将旋转90度并显示为拉伸,因为纵横比(1200 x 1600像素)未更改...我正在使用它从Camera获取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的大小?
查看完整描述

3 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

您的代码不起作用的原因是因为没有考虑您所拥有的代码上的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(),因此它存在线程不安全的问题。由于上述代码仅使用CG功能,因此已全部准备就绪。我还有一个类似的功能,可以调整大小并在图像上进行适当的纵横比填充-让我知道这是否是您要的内容。


查看完整回答
反对 回复 2019-12-18
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

我那里的一些代码也遇到了这个问题,我发现此代码有效,请检查一下,让我知道您找到了什么


+ (UIImage*)imageWithImage:(UIImage*)image 

               scaledToSize:(CGSize)newSize;

{

   UIGraphicsBeginImageContext( newSize );

   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();


   return newImage;

}


查看完整回答
反对 回复 2019-12-18
?
白板的微信

TA贡献1883条经验 获得超3个赞

如果要通过在矩形图像(水平或垂直)中通过在两侧的宽度或高度进行裁剪来制作正方形图像,请使用我的代码。所不同的是,我不会伸展,但会收割。我通过修改从最上面的代码中获得了它:


//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);

}


查看完整回答
反对 回复 2019-12-18
  • 3 回答
  • 0 关注
  • 532 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信