Using ALAssetsLibrary and ALAsset take out Image as NSData

后端 未结 3 1558
青春惊慌失措
青春惊慌失措 2020-12-13 03:01

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.

3条回答
  •  -上瘾入骨i
    2020-12-13 03:24

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

提交回复
热议问题