2012-01-09 12 views
14

io non sono in grado di recuperare NSData dall'URL che ricevo da ALAssetottenere il video NSData da ALAsset url iOS

Di seguito è riportato il codice che ho provato: - Ho sempre trovato NSData come nullo.

NSData *webData = [NSData dataWithContentsOfURL:[asset defaultRepresentation].url]; 

Ho anche provato qualcosa di simile

NSData *webData1 = [NSData dataWithContentsOfURL:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]]]; 

L'url che ricevo dalla ALAsset: -?

asset-biblioteca: //asset/asset.MOV id = 1.000.000,116 mila & ext = MOV

Ho provato questo link qui sotto che funziona ma ho bisogno di scrivere inutilmente in una posizione temporanea che richiede molto tempo.

Getting video from ALAsset

Qualsiasi suggerimento in direzione giusta sarebbe molto apprezzato.

In attesa di vostre risposte

risposta

60

provare questo codice: -

ALAssetRepresentation *rep = [asset defaultRepresentation]; 
Byte *buffer = (Byte*)malloc((NSUInteger)rep.size); 
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:(NSUInteger)rep.size error:nil]; 
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES]; 
+0

grazie ha funzionato :) Spero che da quando si r utilizzando dataWithBytesNoCopy: freeWhenDone: non abbiamo bisogno di liberare il "buffer" in modo esplicito? – Ekra

+1

non creerà alcun problema. – Leena

+1

funzionante bene ....... leeeeena .... :) – Rajneesh071

17

Byte tampone = (Byte) malloc (rep.size); se il rep.size è così grande, forse 300M, questo andrà in crash. in modo da provare questo codice:

+ (BOOL)writeDataToPath:(NSString*)filePath andAsset:(ALAsset*)asset 
{ 
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; 
    NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath]; 
    if (!handle) { 
     return NO; 
    } 
    static const NSUInteger BufferSize = 1024*1024; 

    ALAssetRepresentation *rep = [asset defaultRepresentation]; 
    uint8_t *buffer = calloc(BufferSize, sizeof(*buffer)); 
    NSUInteger offset = 0, bytesRead = 0; 

    do { 
     @try { 
      bytesRead = [rep getBytes:buffer fromOffset:offset length:BufferSize error:nil]; 
      [handle writeData:[NSData dataWithBytesNoCopy:buffer length:bytesRead freeWhenDone:NO]]; 
      offset += bytesRead; 
     } @catch (NSException *exception) { 
      free(buffer); 

      return NO; 
     } 
    } while (bytesRead > 0); 

    free(buffer); 
    return YES; 
} 
+0

Grazie. Ma come posso aggiungere i valori dei metadati qui? Ho davvero bisogno di scriverli. –

Problemi correlati