本文介绍了从UIImagePickerControlleHow的结果中,如何获取包含元数据的JPEG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 4.2上,当我使用UIImagePickerController让用户从照片库中选择图像时,这些是返回给我的字典键:

On iOS 4.2 when I use UIImagePickerController to let the user select a image from the photo library these are the dictionary keys that are returned to me:

2011-03-02 13:15:59.518 xxx[15098:307] didFinishPickingMediaWithInfo: 
  info dictionary: {
    UIImagePickerControllerMediaType = "public.image";
    UIImagePickerControllerOriginalImage = "<UIImage: 0x3405d0>";
    UIImagePickerControllerReferenceURL = 
      "assets-library://asset/asset.JPG?id=1000000050&ext=JPG";
}

使用这些键中的一个或多个,如何获得包含图像元数据(例如曝光信息和GPS位置数据)的JPEG表示,以便可以将其上载到某个位置并包含元数据(不剥离) )?

Using one or more of these keys, how can I get a JPEG representation that would include the image metadata (such as exposure information and GPS location data) such that I can upload that somewhere and have the metadata included (not stripped off)?

沃伦·伯顿(Warren Burton)在>显示图像中给出的非常好的答案中看到了从iPhone中从ALAsset检索到的URL中获取信息?如何使用UIImagePickerControllerReferenceURL和ALAssetsLibrary assetForURL方法获取ALAsset和ALAssetRepresentation.但是我该怎么办才能获取其中包含所有元数据的JPEG?

I see from Warren Burton's very nice answer in Display image from URL retrieved from ALAsset in iPhone? how to use the UIImagePickerControllerReferenceURL and the ALAssetsLibrary assetForURL method to get to the ALAsset and the ALAssetRepresentation. But what do I do then to get to the JPEG that includes in it all the metadata?

还是通过UIImage有一种机制?

Or is there a mechanism through the UIImage?

最重要的是,我想获取包含其中的元数据的JPEG ...

The bottom line here is that I want to get JPEG with the metadata included in it...

推荐答案

自从我问了这个问题以来,我做了更多的实验,并认为我现在知道答案了.所有结果都是在iOS 4.2上获得的,这是我关心的全部...

Since I asked the question I have done some more experimentation and think I know the answer now. All results were gotten on iOS 4.2 which is all I care about...

首先,我们使用的是UIImageJPEGRepresentation ala:

First of all, we were using UIImageJPEGRepresentation ala:

NSData *imageData = UIImageJPEGRepresentation(self.selectedImage, 0.9);

似乎并不能为您提供(大部分)图像中的元数据(EXIF,GPS等).很公平,我认为那是众所周知的.

which seems to not give you (much of) the metadata (EXIF, GPS, etc.) that is in the image. Fair enough and I think that's well-known.

我的测试表明,图像资产的默认表示形式"中的JPEG将包含所有元数据,包括EXIF和GPS信息(假设它位于第一位).通过从资产URL到资产到资产的默认表示形式(ALAssetRepresentation),然后使用getBytes方法/消息检索JPEG图像的字节,可以获取该图像.该字节流中包含上述元数据.

My testing shows that the JPEG in the "default representation" for the image asset will contain all the metadata, including EXIF and GPS information (assuming it's there in the first place). You can get that image by going from the asset URL to the Asset to the asset's default representation (ALAssetRepresentation) and then using the getBytes method/message to retrieve the bytes for the JPEG image. That stream of bytes has the aforementioned metadata in it.

这是我用于此的一些示例代码.它采用一个资产URL(假定是用于图像),并返回带有JPEG的NSData.关于您的使用,代码中的错误处理等方面的警告免责声明.

Here's some example code that I use for this. It takes an Asset URL, presumed to be for an image, and returns NSData with with the JPEG. Caveat emptor with respect to your use, error handling in the code, etc.

/*
 * Example invocation assuming that info is the dictionary returned by 
 * didFinishPickingMediaWithInfo (see original SO question where
 * UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=1000000050&ext=JPG").
 */
[self getJPEGFromAssetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]];
// ...

/* 
 * Take Asset URL and set imageJPEG property to NSData containing the
 * associated JPEG, including the metadata we're after.
 */
-(void)getJPEGFromAssetForURL:(NSURL *)url {
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:url
        resultBlock: ^(ALAsset *myasset) {
            ALAssetRepresentation *rep = [myasset defaultRepresentation];
#if DEBUG
            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]);
#endif

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

这篇关于从UIImagePickerControlleHow的结果中,如何获取包含元数据的JPEG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 17:11