首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CGImageSource/CGImageDestination在磁盘上旋转CGImage?

使用CGImageSource/CGImageDestination在磁盘上旋转CGImage?
EN

Stack Overflow用户
提问于 2011-05-23 16:58:41
回答 1查看 3K关注 0票数 3

我正在开发一个应用程序,它需要使用UIImagePickerController拍照,在应用程序中显示缩略图,并使用ASIFormDataRequest (来自ASIHTTPRequest)将图片提交给服务器。

我想使用来自ASIFormDataRequest的ASIFormDataRequest,因为在我的经验中,它比尝试使用UIImageJPEGRepresentation提交图像和提交原始NSData要快。为此,我使用CGImageDestination并使用url创建一个图像目的地,将该映像保存到磁盘,然后将该文件上传到磁盘上。

为了创建缩略图,我使用了CGImageSourceCreateThumbnailAtIndex (参见文档)并使用我刚刚保存的文件的路径创建了一个图像源。

我的问题是,无论我传递到图像目的地或缩略图创建调用什么选项,我的缩略图总是逆时针旋转90度。上传的图像也是旋转的。我已经尝试过使用CGImageDestinationSetProperties显式地设置图像选项中的方向,但它似乎不需要。我找到的唯一解决方案是在内存中旋转图像,但我真的想避免这种情况,因为这样做会使完成thumbnail+saving操作所需的时间翻一番。理想情况下,我希望能够在磁盘上旋转图像。

我在使用CGImageDestinationCGImageSource时遗漏了什么吗?我会在下面贴一些代码。

保存图像:

代码语言:javascript
复制
NSURL *filePath = [NSURL fileURLWithPath:self.imagePath];
CGImageRef imageRef = [self.image CGImage];
CGImageDestinationRef ref = CGImageDestinationCreateWithURL((CFURLRef)filePath, kUTTypeJPEG, 1, NULL);
CGImageDestinationAddImage(ref, imageRef, NULL);

NSDictionary *props = [[NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithFloat:1.0], kCGImageDestinationLossyCompressionQuality,
                        nil] retain];
//Note that setting kCGImagePropertyOrientation didn't work here for me

CGImageDestinationSetProperties(ref, (CFDictionaryRef) props);

CGImageDestinationFinalize(ref);
CFRelease(ref);

生成缩略图

代码语言:javascript
复制
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)filePath, NULL);
if (!imageSource)
    return;

CFDictionaryRef options = (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
                                            (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                                            (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
                                            (id)[NSNumber numberWithFloat:THUMBNAIL_SIDE_LENGTH], (id)kCGImageSourceThumbnailMaxPixelSize, nil];
CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options);
UIImage *thumb = [UIImage imageWithCGImage:imgRef];

CGImageRelease(imgRef);
CFRelease(imageSource);

然后上传我刚才用的图片

代码语言:javascript
复制
[request setFile:path withFileName:fileName andContentType:contentType forKey:@"photo"];

其中,path是指向上面代码保存的文件的路径。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-28 02:51:12

据我所知,在尝试了许多不同的事情之后,这不能用当前的公共API完成,而必须在内存中完成。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6100425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档