旋转CGImage

编程入门 行业动态 更新时间:2024-10-27 07:23:34
本文介绍了旋转CGImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个UIImage,我是从UIImagePickerController获取的。当我从 - (void)imagePickerController接收图像时:(UIImagePickerController *)选择器didFinishPickingMediaWithInfo:(NSDictionary *)info 方法我把它直接放到UIImageView中进行预览并给出用户可选择保存或放弃图像。

I have a UIImage that I'm getting from a UIImagePickerController. When I receive the image from the - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method I put it straight into a UIImageView for preview and give the user the option to either save or discard the image.

当用户选择保存选项时,应用程序将获取图像的png数据并将其保存到文档目录中。但是在2种可能的设备方向之一(仅景观)下发生的事情是图像被颠倒保存(更具体地说,从它应该的位置旋转180度)。因此,当我在图库中加载图片时,它会显示为颠倒。

When the user selects the save option the application get the png data for the image and saves it to it's documents directory. But what happens under one of the 2 possible device orientations (landscape only) is that the image is saved upside down (more specifically, rotated 180 degrees from what it should be). So when I load the image in the gallery it appears upside down.

(请参阅图库中的左下角图片)

(See the bottom left image in the gallery)

我已经解决了UIImage来自UIImage的UIImage中的原始图像数据没有旋转,而是将方向存储为对象的属性,仅在显示时应用。所以我试图使用我在网上找到的一些代码来旋转与UIImage相关联的CGImage。但它似乎对图像完全没有影响。我正在使用的代码如下:

I have worked this out to be that the raw image data in the UIImage from the UIImagePickerController is not rotated, rather, the orientation is stored as a property on the object and only applied when displaying. So I'm trying to rotate the CGImage associated with the UIImage using some code I found online. But it appears to have absolutely no affect on the image. The code I'm using follows:

- (void)rotateImage:(UIImage*)image byRadians:(CGFloat)rads { // calculate the size of the rotated view's containing box for our drawing space UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)]; CGAffineTransform t = CGAffineTransformMakeRotation(rads); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; // Create the bitmap context UIGraphicsBeginImageContext(rotatedSize); CGContextRef bitmap = UIGraphicsGetCurrentContext(); // Move the origin to the middle of the image you want to rotate and scale around the center. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); // Rotate the image context CGContextRotateCTM(bitmap, rads); // Now, draw the rotated/scaled image into the context CGContextScaleCTM(bitmap, 1.0, -1.0); CGContextDrawImage(bitmap, CGRectMake(image.size.width / 2, image.size.height / 2, image.size.width, image.size.height), [image CGImage]); image = UIGraphicsGetImageFromCurrentImageContext(); image = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationDown]; UIGraphicsEndImageContext(); }

我想知道为什么这段代码无效,因为每件我在网上找到这个图像旋转的代码看起来不起作用。

I would like to know why this code isn't working, since every piece of code I find online to do this image rotation appears to not work.

推荐答案

-(UIImage*) rotate:(UIImage*) src andOrientation:(UIImageOrientation)orientation { UIGraphicsBeginImageContext(src.size); CGContextRef context=(UIGraphicsGetCurrentContext()); if (orientation == UIImageOrientationRight) { CGContextRotateCTM (context, 90/180*M_PI) ; } else if (orientation == UIImageOrientationLeft) { CGContextRotateCTM (context, -90/180*M_PI); } else if (orientation == UIImageOrientationDown) { // NOTHING } else if (orientation == UIImageOrientationUp) { CGContextRotateCTM (context, 90/180*M_PI); } [src drawAtPoint:CGPointMake(0, 0)]; UIImage *img=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; }

使用这个..它完美无缺!从UIImagePicker中选择图像时,请确保使用此功能:

use this one .. it works perfect ! Just make sure you use this function when you are picking images from UIImagePicker:

示例:

UIImage *image; image = [self rotate:image andOrientation:image.imageOrientation];

确保在从文档目录中选择照片的位置使用此功能。

Make sure to use this function in place where you pick photos from documents directory.

更多推荐

旋转CGImage

本文发布于:2023-07-30 16:21:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1251003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CGImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!