ALAsset,将照片发送到Web服务,包括其exif数据

时间:2011-07-30 08:05:00

标签: iphone exif alasset

我想将相机胶卷中的照片发送到网络服务 - 包括其exif数据。 我正在使用ASIFormDataRequest - 所以我这样做:

ASIFormDataRequest *request = [[ASIFormDataRequest alloc]initWithURL:url];

为了节省内存我直接想发送文件:

[request addFile:localPath forKey:@"image"];

所以我需要资产的本地路径。 我想我无法获得资产的本地路径,因此我暂时将资产保存到文件中:

ALAsset* selectedAsset = [assets objectAtIndex:index];
CGImageRef imageRef = selectedAsset.defaultRepresentation.fullScreenImage;
UIImage* image = [UIImage imageWithCGImage:imageRef];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *cachesDirectory = [paths objectAtIndex:0];

NSData* imageData = UIImagePNGRepresentation(image);
NSString* filePath = [NSString stringWithFormat:@"%@/imageTemp.png",cachesDirectory];
[imageData writeToFile:filePath atomically:YES];

然后我用这条路径来做

[request addFile:localPath forKey:@"image"];

图像被发送到服务器 - 但没有我需要的exif数据。 除此之外,我认为必须有一个更聪明的方法来做到这一点。

TIA

1 个答案:

答案 0 :(得分:10)

好吧 - 我想我想通了。诀窍是使用defaultRepresentaion的原始数据:

ALAsset* selectedAsset = [assets objectAtIndex:index];

int byteArraySize = selectedAsset.defaultRepresentation.size;

NSMutableData* rawData = [[NSMutableData alloc]initWithCapacity:byteArraySize];
void* bufferPointer = [rawData mutableBytes];

NSError* error=nil;
[selectedAsset.defaultRepresentation getBytes:bufferPointer fromOffset:0 length:byteArraySize error:&error];
if (error) {
    NSLog(@"%@",error);
}
rawData = [NSMutableData dataWithBytes:bufferPointer length:byteArraySize];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString* filePath = [NSString stringWithFormat:@"%@/imageTemp.png",cachesDirectory];
[rawData writeToFile:filePath atomically:YES];

使用路径将图像发送到服务器后,服务器上的文件会保留所有exif数据