开发者

rotate CGImage on disk using CGImageSource/CGImageDestination?

I'm working on an application that needs to take a picture using UIImagePickerController, display a thumbnail of it in the app, and also submit that picture to a server using ASIFormDataRequest (from ASIHTTPRequest).

I want to use setFile:withFileName:andContentType:forKey: from ASIFormDataRequest since in my experience it's faster than trying to submit an image using UIImageJPEGRepresentation and submitting raw NSData. To that end I'm using CGImageDestination and creating an image destination with a url, saving that image to disk, and then uploading that file on disk.

In order to create the thumbnail I'm using CGImageSourceCreateThumbnailAtIndex (see docs) and creating an image source with the path of the file I just saved.

My problem is that no matter what options I pass into the image destination or the thumbnail creation call, my thumbnail always comes out rotated 90 degrees counterclockwise. The uploaded image is also rotated. I've tried explicitly setting the orientation in the options of the image using CGImageDestinationSetProperties but it doesn't seem to take. The only solution I've found is to rotate the image in memory, but I really want to avoid that since doing so doubles the time it takes for the thumbnail+saving operation to finish. Ideally I'd like to be able to rotate the image on disk.

Am I missing something in how I'm using CGImageDestination or CGImageSource? I'll post some code below.

Saving the image:

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

Generating the thumbnail

CGImageSourceRef imageSource =开发者_Go百科 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);

And then to upload the image I just use

[request setFile:path withFileName:fileName andContentType:contentType forKey:@"photo"];

where path is the path to the file saved with the code above.


As far as I know and after trying lots of different things, this cannot be done with current public APIs and has to be done in memory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜