1

I am trying to migrate my code from ALAssetLibrary to Photos Framework, but I have a problem when I write a file to the sandbox, the size of the files generated do not match.

Writing file using ALAssetRepresentation:

[[NSFileManager defaultManager] createFileAtPath:localFilePath contents:nil attributes:nil];  
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:localFilePath];  

static const NSUInteger kBufferSize = 10 * 1024;  
uint8_t *buffer = calloc(kBufferSize, sizeof(*buffer));  
NSUInteger offset = 0, bytesRead = 0;  

do {  
     bytesRead = [assetRepresentation getBytes:buffer fromOffset:offset length:kBufferSize error:nil];  
     [handle writeData:[NSData dataWithBytesNoCopy:buffer length:bytesRead freeWhenDone:NO]];  
     offset += bytesRead;  
} while (bytesRead > 0);  

free(buffer);  
[handle closeFile];

Writing a file using AVAssetExportSession:

PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];  
options.version = PHVideoRequestOptionsVersionOriginal;  
options.networkAccessAllowed = YES;  

[[PHImageManager defaultManager] requestExportSessionForVideo:_phasset options:options exportPreset:AVAssetExportPresetPassthrough  
                                                           resultHandler:^(AVAssetExportSession *exportSession, NSDictionary *info) {  
             exportSession.outputURL = [NSURL fileURLWithPath:localFilePath];  
             exportSession.outputFileType = AVFileTypeQuickTimeMovie;  
             [exportSession exportAsynchronouslyWithCompletionHandler:  
     }];  
}];

The problem is that the output files are not exactly equals, when I reproduce the videos seem to be the same video, but the size of the files do not match. For example the size of a short video (2 seconds) created using ALAssetRepresentation is 229KB and with AVAssetExportSession is 227KB.

The files generated should be equals because I use a CRC based on the content of the files.

Thank you in advance.

  • BTW, using `- (void)writeDataForAssetResource:(PHAssetResource *)resource toFile:(NSURL *)fileURL options:(PHAssetResourceRequestOptions *)options completionHandler:(void (^)(NSError *error))completionHandler` writes exactly the same file that using `ALAssetRepresentation`, the problem is that this is only available for iOS 9.0 and if the asset is in iCloud looks like that this method has a bug and doesn't download the asset. – Javier Navarro Apr 06 '16 at 08:02

0 Answers0