2014-06-25 2 views
7

Ich habe dieses Problem seit mehr als einer Woche konfrontiert, ich bin nicht in der Lage, Original-Foto/Video-Pfad von Assets-Library URL zu bekommen.Wie bekomme ich den Ursprungspfad für Fotos/Videos von der Assets-Bibliothek in iOS/iPhone?

Ich kann seine Daten erhalten, indem Sie diese Datei in meiner SandBox-App speichern, aber hier möchte ich diese Dinge vermeiden, um erneut eine Kopie dieser Datei zu erstellen.

Ich habe dies in DropBox investieren, sie sind direkt von Assets-Bibliothek URL hochladen. Also, bitte hilf mir, dieses Problem zu lösen.

Vielen Dank im Voraus.

Hier ist mein Code:

-(NSString*) videoAssetURLToTempFile:(NSString*)combineURL{ 
self.activityIndicator.hidden = FALSE; 
NSLog(@"combineURL: %@",combineURL); 
NSArray *arr = [combineURL componentsSeparatedByString:@"->"]; 
NSString *index = [arr objectAtIndex:0]; 

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:1]]]; 

//url like: "assets-library://asset/asset.MOV?id=78988A2B-203B-41B9-8EDA-F3029303DFBF&ext=MOV" 

NSString * surl = [url absoluteString]; 
NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4]; 

NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate]; 
NSString *fname = [NSString stringWithFormat: @"%f.%@",ti,ext]; 

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"SAVED_PHOTOS"]; 
// New Folder is your folder name 
NSError *error = nil; 
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error]; 

NSString *tmpfile = [stringPath stringByAppendingPathComponent:fname]; 

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){ 
    ALAssetRepresentation * rep = [myasset defaultRepresentation]; 
    NSLog(@"video url: %@",rep.url); 
    NSUInteger size = [rep size]; 

    NSLog(@"file size: %@",[NSString stringWithFormat:@"%llu",(unsigned long long)rep.size]); 
    if (!appDelegate.arrFileSize) { 
     appDelegate.arrFileSize = [[NSMutableArray alloc] init]; 
    } 
    [appDelegate.arrFileSize replaceObjectAtIndex:[index integerValue] withObject:[NSString stringWithFormat:@"%llu",(unsigned long long)rep.size]]; 
    [appDelegate.accessToken setObject:appDelegate.arrFileSize forKey:@"FileSize"]; 
    [appDelegate.accessToken synchronize]; 
    NSLog(@"video fileSize: %@",[self convertbyteToKB_MB:(unsigned long long)rep.size]); 
    unsigned long long freeSpaceSize = [self getFreeDiskspace]; 
    NSLog(@"freeSpaceSize: %llu",freeSpaceSize); 

    if (freeSpaceSize<rep.size && rep.size<1073741824) { 
     NSString *alertMsg; 
     if (IS_IPHONE) { 
      alertMsg = [NSString stringWithFormat:@"App requires %@ free storage space to upload this video. To proceed with this upload, please go to your iPhone's Settings and clear some space.",[self convertbyteToKB_MB:(unsigned long long)rep.size]]; 
     } 
     else{ 
      alertMsg = [NSString stringWithFormat:@"App requires %@ free storage space to upload this video. To proceed with this upload, please go to your iPad's Settings and clear some space.",[self convertbyteToKB_MB:(unsigned long long)rep.size]]; 
     } 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:alertMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [alert show]; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       self.waitToUploadLbl.hidden = TRUE; 
      }); 
     }); 

    } 
    else{ 
     if (rep.size>=1073741824) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       self.waitToUploadLbl.hidden = TRUE; 
      }); 
     } 
     else{ 
      convertingTotal++; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       self.waitToUploadLbl.hidden = FALSE; 
      }); 
      const int bufferSize = 8*1024*1024; 
      FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+"); 
      if (f == NULL) { 
       return; 
      } 

      Byte * buffer = (Byte*)malloc(bufferSize); 
      unsigned long long read = 0, offset = 0, written = 0; 
      NSError* err; 
      if (size != 0) { 

       do { 
        read = (unsigned long long)[rep getBytes:buffer 
                fromOffset:offset 
                 length:bufferSize 
                 error:&err]; 
        written = (unsigned long long)fwrite(buffer, sizeof(char), read, f); 
        offset += read; 
       } while (read != 0); 
       blockedNo++; 
      } 
      fclose(f); 
      NSLog(@"file saved to temp"); 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       NSLog(@"total: %d blockedNo: %d",convertingTotal,blockedNo); 
       self.waitToUploadLbl.hidden = TRUE; 
      }); 
     } 
    } 

}; 

ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
{ 
    //NSLog(@"Can not get asset - %@",[myerror localizedDescription]); 

}; 

if(url) 
{ 
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 
    [assetslibrary assetForURL:url 
        resultBlock:resultblock 
        failureBlock:failureblock]; 
} 
self.activityIndicator.hidden = TRUE; 
@try { 
    [appDelegate.uploadArray1 replaceObjectAtIndex:[index integerValue] withObject:tmpfile]; 
    [appDelegate.accessToken setObject:appDelegate.uploadArray1 forKey:@"UploadArray"]; 
    [appDelegate.accessToken synchronize]; 
    NSLog(@"temporary path: %@",tmpfile); 
    [self performSelector:@selector(uploadphotoToServer:) withObject:index afterDelay:1]; 
} 
@catch (NSException *exception) { 
    NSLog(@"Error: %@",exception); 
} 
@finally { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     if (convertingTotal==blockedNo) { 
      self.waitToUploadLbl.hidden = TRUE; 
     } 

    }); 
} 
return tmpfile; 
} 
+0

Dies ist, wie ich das erste Video in den gewünschten Ordner speichern verwaltet und dann auf den Server laden von dieser Pfad. Ich möchte diesen Speichervorgang entfernen und direkt von "assets-library: //asset/asset.MOV? Id = 22193DF9-2B64-4E43-AB01-A16E356FD2E6 & ext = MOV" diese URL hochladen. –

+0

Ja, Sie können versuchen, eine Videodatei um 500 MB hochzuladen. Dadurch wird die App-Nutzung in den Einstellungen nicht erhöht. Es bleibt gleich, und wenn ich die gleiche Videodatei in meiner App versuchte, zeigte es 500MB + appSize, insgesamt 553MB in Verwendung. –

Antwort

2

ich das hoffen, dass Sie helfen

NSString *str_url=[arrURL objectAtIndex:indexPath.row];//put your index 

        ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init]; 

        NSURL* aURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",str_url]]; 

        [assetLibrary assetForURL:aURL resultBlock:^(ALAsset *asset) { 

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

         cell.imageCell.image=[UIImage imageWithData:data]; 


        } failureBlock:^(NSError *error) 
        { 
         NSLog(@"Error: %@",[error localizedDescription]); 
        }]; 
+0

Danke für Ihre Antwort. Aber ich möchte Original-Bild/Video-Datei hochladen. Wenn ich NSData für den Video-Upload nehme, der etwa 500 MB groß ist, gibt die App die Speicherwarnung zurück und stürzt dann ab. Auch UI reagiert nicht. Ich möchte einen Pfad von wo ich direkt eine Datei hochladen kann, ohne diese Datei in den Speicher oder NSDirectory zu speichern. –

Verwandte Themen