18

I wish to extract the image using ALAssetsLibrary and ALAsset directly in the form of a NSData object.

Using a NSURL I take out the image in the following manner.

NSURL *referenceURL =newURL;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset)
{
     UIImage  *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
}

Now here we take the image as a UIImage, but I need to take the image directly as NSData.

I wish to do this because (I have read that) once you take the image in UIImage, then we lose all the EXIF details of the Image.

That's the reason I want to extract the image directly as NSData, instead of doing this

NSData *webUploadData=UIImageJPEGRepresentation(copyOfOriginalImage, 0.5);

This step makes me lose all the EXIF details.

Please Help.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
Shishir Shetty
  • 2,021
  • 3
  • 20
  • 35

3 Answers3

33
        ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
        [assetLibrary assetForURL:[[self.imagedata objectAtIndex:i] resultBlock:^(ALAsset *asset) 
        {
            ALAssetRepresentation *rep = [asset defaultRepresentation];
            Byte *buffer = (Byte*)malloc(rep.size);
            NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
            NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
            [data writeToFile:photoFile atomically:YES];//you can save image later
        } 
        failureBlock:^(NSError *err) {
            NSLog(@"Error: %@",[err localizedDescription]);
        }];
Thizzer
  • 16,153
  • 28
  • 98
  • 139
Dhaval Panchal
  • 2,529
  • 1
  • 25
  • 36
  • 1
    Thank you for your wonderful suggestion. But, is there any way I can compress the image ? Before using it as NSData. – Shishir Shetty Dec 13 '11 at 13:38
  • If I understand right, the image will already be compressed. The default presentation will probably be a jpeg or png – HeikoG Dec 15 '11 at 07:36
  • 2
    i have a problem with this technique while importing multiple ALAssets at the same tim, it seems as if the buffer gets reused for the next item. – Nicolas Manzini Dec 20 '13 at 15:27
  • this answer is just fantastic! thanks for the code! The CGImage method also has a huge memory footprint compared to this method. – dreampowder Apr 10 '14 at 14:48
  • I have used this code to get GIF image, this works but not properly the animation state is not staying with the GIF image and its size get compressed,any other option will be useful. – Pankaj Bhardwaj Jul 04 '15 at 17:51
  • @PankajBhardwaj did you ever able to get succes in getting gif from alasset ? – maddy Jul 16 '15 at 02:58
  • 1
    No @Alok not gif I got PNG and JPG , Even i have tried to save in document directory but still its just a image without animation. – Pankaj Bhardwaj Jul 16 '15 at 19:17
  • i have been able to do it successfully. will share code when on my mac. – maddy Jul 16 '15 at 22:20
0

Using this code:

+ (BOOL)exportDataToURL:(NSString *)filePath error:(NSError **)error andAsset:(ALAsset *)asset
{
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];

    if (!handle)
        return NO;

    static const NSUInteger BufferSize = 1024 * 1024;

    ALAssetRepresentation *rep = [asset defaultRepresentation];
    uint8_t *buffer = calloc(BufferSize, sizeof(*buffer));
    NSUInteger offset = 0, bytesRead = 0;

    do {
        @try {
            bytesRead = [rep getBytes:buffer fromOffset:offset length:BufferSize error:error];
            [handle writeData:[NSData dataWithBytesNoCopy:buffer length:bytesRead freeWhenDone:NO]];
            offset += bytesRead;
        } @catch(NSException *exception) {
            free(buffer);

            return NO;
        }
    } while (bytesRead > 0);

    free(buffer);
    return YES;
}
Yong Li
  • 266
  • 1
  • 7
-1
UIImage * selImage = [UIImage imageWithCGImage:[asset thumbnail]];       
NSData *baseImage=UIImagePNGRepresentation(selImage);
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
vni
  • 56
  • 5