You can get photo raw binary with below implementation and save to your target file.
+ (NSData *)photoAssetRawData:(ALAsset *)photoAsset error:(NSError **)error {
ALAssetRepresentation *rep = photoAsset.defaultRepresentation;
NSMutableData *data = [NSMutableData new];
long long offset = 0;
uint8_t dataBuffer[PHOTO_READ_CHUNK_SIZE];
NSError *internalError;
do {
NSUInteger readByteLength = [rep getBytes:dataBuffer fromOffset:offset length:sizeof(dataBuffer) error:&internalError];
if(internalError != nil) {
if(error != NULL) {
*error = internalError;
}
return nil;
}
offset += readByteLength;
[data appendBytes:(void*)dataBuffer length:readByteLength];
}
while (offset < rep.size);
return data;
}
One thing must be aware, this raw data has not applied any filter iOS default gallery App added, if you want these filter applied, you should get these XMP liked filter from [ALAssetRepresentation metadata] and create filters with [CIFilter filterArrayFromSerializedXMP:inputImageExtent:error:], then apply them on full resolution image, finally save this processed image as JPEG or PNG to file.
Below shows how to apply these filters.
+ (CGImageRef)applyXMPFilter:(ALAsset *)asset{
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef imageRef = [rep fullResolutionImage];
NSString *adjustmentXMP;
NSData *adjustmentXMPData;
NSError *__autoreleasing error = nil;
NSArray *filters=nil;
CGRect extend = CGRectZero;
//add filter to image
ALAssetRepresentation *representation = asset.defaultRepresentation;
adjustmentXMP = [representation.metadata objectForKey:@"AdjustmentXMP"];
adjustmentXMPData = [adjustmentXMP dataUsingEncoding:NSUTF8StringEncoding];
extend.size = representation.dimensions;
filters = [CIFilter filterArrayFromSerializedXMP:adjustmentXMPData inputImageExtent:extend error:&error];
if(filters)
{
CIImage *image = [CIImage imageWithCGImage:imageRef];
CIContext *context = [CIContext contextWithOptions:nil];
for (CIFilter *filter in filters)
{
[filter setValue:image forKey:kCIInputImageKey];
image = [filter outputImage];
}
imageRef = [context createCGImage:image fromRect:image.extent];
}
return imageRef;
}