Search code examples
iphoneexifalasset

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


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


Solution

  • 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