Pergunta

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.

Foi útil?

Solução

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

Outras dicas

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;
}
UIImage * selImage = [UIImage imageWithCGImage:[asset thumbnail]];       
NSData *baseImage=UIImagePNGRepresentation(selImage);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top