Wednesday 7 March 2012

Read the file from assets-library and write into ur custom file


Although u can see many same solutions available same as given below...

This method take the Asset and fileName to create that file into the tempdirectory. 
It returns the path of the tempdirectory/file where it stores the 


This is the same as reading a file from the Assets library (Photo library). 


-(NSString*) writeVideoFileIntoTemp:(NSString*)fileName andAsset:(ALAsset*)asset
{
    NSString * tmpfile = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];

        
        ALAssetRepresentation * rep = [asset defaultRepresentation];
        
        NSUInteger size = [rep size];
        const int bufferSize = 1024*1024; // or use 8192 size as read from other posts 
        
        NSLog(@"Writing to %@",tmpfile);
        FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+");
        if (f == NULL) {
            NSLog(@"Can not create tmp file.");
            return;
        }
        
        Byte * buffer = (Byte*)malloc(bufferSize);
        int read = 0, offset = 0, written = 0;
        NSError* err;
        if (size != 0) {
            do {
                read = [rep getBytes:buffer
                          fromOffset:offset
                              length:bufferSize 
                               error:&err];
                written = fwrite(buffer, sizeof(char), read, f);
                offset += read;
            } while (read != 0);
            
            
        }
        fclose(f);
    return tmpfile;
}

No comments:

Post a Comment