我正在开发一个应用程序,它需要使用UIImagePickerController拍照,在应用程序中显示缩略图,并使用ASIFormDataRequest (来自ASIHTTPRequest)将图片提交给服务器。
我想使用来自ASIFormDataRequest的ASIFormDataRequest,因为在我的经验中,它比尝试使用UIImageJPEGRepresentation提交图像和提交原始NSData要快。为此,我使用CGImageDestination并使用url创建一个图像目的地,将该映像保存到磁盘,然后将该文件上传到磁盘上。
为了创建缩略图,我使用了CGImageSourceCreateThumbnailAtIndex (参见文档)并使用我刚刚保存的文件的路径创建了一个图像源。
我的问题是,无论我传递到图像目的地或缩略图创建调用什么选项,我的缩略图总是逆时针旋转90度。上传的图像也是旋转的。我已经尝试过使用CGImageDestinationSetProperties显式地设置图像选项中的方向,但它似乎不需要。我找到的唯一解决方案是在内存中旋转图像,但我真的想避免这种情况,因为这样做会使完成thumbnail+saving操作所需的时间翻一番。理想情况下,我希望能够在磁盘上旋转图像。
我在使用CGImageDestination或CGImageSource时遗漏了什么吗?我会在下面贴一些代码。
保存图像:
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);生成缩略图
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);然后上传我刚才用的图片
[request setFile:path withFileName:fileName andContentType:contentType forKey:@"photo"];其中,path是指向上面代码保存的文件的路径。
发布于 2011-09-28 02:51:12
据我所知,在尝试了许多不同的事情之后,这不能用当前的公共API完成,而必须在内存中完成。
https://stackoverflow.com/questions/6100425
复制相似问题