iOS 4 Image Oriented Incorrectly When Posted To Server

北慕城南 提交于 2019-12-08 09:58:44

问题


I am a relatively new iOS developer. I am attempting to build an app that uploads images to a server. However, when I get the image with metadata, the orientation is always off. Here is the code I am using:

- (void)getJPEGFromAssetForURL:(NSURL *)url {
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:url
               resultBlock: ^(ALAsset *myasset) {
                   self.rep = [myasset defaultRepresentation];

                   NSLog(@"getJPEGFromAssetForURL: default asset representation for %@: uti: %@ size: %lld url: %@ orientation: %d scale: %f metadata: %@", 
                         url, [rep UTI], [rep size], [rep url], [rep orientation], 
                         [rep scale], [rep metadata]);


                   Byte *buf = malloc([rep size]);  // will be freed automatically when associated NSData is deallocated
                   NSError *err = nil;
                   NSUInteger bytes = [rep getBytes:buf fromOffset:0LL 
                                             length:[rep size] error:&err];
                   if (err || bytes == 0) {
                       // Are err and bytes == 0 redundant? Doc says 0 return means 
                       // error occurred which presumably means NSError is returned.

                       NSLog(@"error from getBytes: %@", err);
                       self.imageJPEG = nil;
                       return;
                   } 
                   self.imageJPEG = [NSData dataWithBytesNoCopy:buf length:[rep size] 
                                                   freeWhenDone:YES];  // YES means free malloc'ed buf that backs this when deallocated
               }
              failureBlock: ^(NSError *err) {
                  NSLog(@"can't get asset %@: %@", url, err);
              }];
[assetslibrary release];
}

When I go ahead and post the image data contained in the imageJPEG variable to my webserver, it comes across with all the metadata intact, but the image is oriented incorrectly.

I also tried the following with the same result:

UIImage *largeimage = [UIImage imageWithCGImage:iref scale:1.0 orientation:[rep orientation]];
NSData *webData = UIImageJPEGRepresentation(largeimage,0.5);

Is there something I'm missing here or this is a bug in the way the images are stored. Please let me know if you can help in any way, I am really excited to get this app out.

Thanks so much.


回答1:


iOS UIImagePickerController result image orientation after upload - This post answered my question!



来源:https://stackoverflow.com/questions/6380376/ios-4-image-oriented-incorrectly-when-posted-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!