发布到服务器时iOS 4图像导向错误

时间:2011-06-17 01:09:26

标签: ios4 uiimagepickercontroller nsdata uiimagejpegrepresentation

我是一个相对较新的iOS开发人员。我正在尝试构建一个将图像上传到服务器的应用程序。但是,当我使用元数据获取图像时,方向始终关闭。这是我正在使用的代码:

- (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];
}

当我继续将imageJPEG变量中包含的图像数据发布到我的网络服务器时,它会完整地覆盖所有元数据,但图像的方向不正确。

我也尝试了以下相同的结果:

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

这里是否有我遗漏的东西,或者这是图像存储方式的错误。如果你能以任何方式提供帮助,请告诉我,我很高兴能把这个应用程序拿出来。

非常感谢。

1 个答案:

答案 0 :(得分:0)