ALAsset , send a photo to a web service including its exif data

为君一笑 提交于 2019-12-19 03:35:31

问题


I want to send a photo from the camera roll to a web services - including its exif data. Im using ASIFormDataRequest - so I do :

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

To save memory I directly want to send the file:

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

So i need the local path of the asset. I think I can not get the local path of an asset, so I temporarily save the asset to a file:

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

Then later I use this path to do the

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

the image gets sent to the server - but without the exif data I need. Besides that, I think there must be a smarter way to do that.

tia


回答1:


ok - i think i figured it out. The trick is to go with the defaultRepresentaion's raw data:

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

After using the path to send the image to the server the file on the server keeps all the exif data



来源:https://stackoverflow.com/questions/6881923/alasset-send-a-photo-to-a-web-service-including-its-exif-data

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