Skip to content

Instantly share code, notes, and snippets.

@ShadoFlameX
Last active September 21, 2016 08:23
Show Gist options
  • Save ShadoFlameX/5132970 to your computer and use it in GitHub Desktop.
Save ShadoFlameX/5132970 to your computer and use it in GitHub Desktop.
Obtain raw image data from an iOS user's photo library
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *assetURL = info[UIImagePickerControllerReferenceURL];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *representation = [asset defaultRepresentation];
Byte *buffer = (Byte *)malloc(representation.size);
NSError *error = nil;
NSUInteger size = [representation getBytes:buffer
fromOffset:0
length:representation.size
error:&error];
if (size == 0) {
NSLog(@"Could not get asset data");
return;
}
NSData *imageData = [NSData dataWithBytesNoCopy:buffer length:size];
// todo: upload data to server
} failureBlock:^(NSError *error) {
NSLog(@"Could not get asset");
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment