2012-11-09 116 views
0

我試圖從我的應用上傳照片到Web服務。從NSURL獲取NSData

我試圖創建的流程如下:

  1. 用戶拍攝的照片與相機
  2. 照片是一個自定義的專輯下保存到相機膠捲
  3. URL保存照片的是給我的商店
  4. 存儲嘗試將照片上傳到Web服務。

我試圖使用NSData *data = [NSData dataWithContentsOfURL:[item assetURL]]其中項目是一個包含有關URL的典範。但該行沒有生產數據,當我登錄它,即使它會產生一個網址:"assets-library://asset/asset.PNG?id=28DBC0AC-21FF-4560-A9D6-5F4BCA190BDB&ext=PNG"

的代碼片段如下:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 



    [self dismissViewControllerAnimated:YES completion:^(void){ 

     [library writeImageToSavedPhotosAlbum:image.CGImage orientation:image.imageOrientation completionBlock:^(NSURL* assetURL, NSError* error) { 

      BCard *card = [[BCard alloc]init]; 

      //error handling 
      if (error!=nil) { 
       NSLog(@"[ERROR] - %@",error); 
       return; 
      } 

      //add the asset to the custom photo album 
      [library addAssetURL: assetURL 
         toAlbum:@"Business Cards" 
      withCompletionBlock:^(NSError *error) { 
       if (error!=nil) { 
        NSLog(@"Custom Album Error: %@", [error description]); 
       } 
      }]; 

      [card setAssetURL:assetURL]; 

      [[BCardStore sharedStore]addToQueue:card]; 
      int index = [[[BCardStore sharedStore]getQueue]count]-1; 

      [[BCardStore sharedStore]uploadItemAtIndex:index withProgressBlock:nil withExitBlock:nil]; 

     }]; 
    }]; 


} 

-(void)uploadItemAtIndex:(NSUInteger)index withProgressBlock:(progressBlock)pBlock withExitBlock:(exitBlock)eBlock 
{ 
    BCard *item = [uploadQueue objectAtIndex:index]; 
    NSURL *url = [NSURL URLWithString:@"http://192.168.0.116:8080"]; 
    NSData *data = [NSData dataWithContentsOfURL:[item assetURL]]; 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url]; 
    numberedName = numberedName +1; 
    NSString *name = [NSString stringWithFormat:@"%d",numberedName]; 

    NSLog(@"%@",[item assetURL]); 

//upload data using AFNetworking here 
} 

的片段[library addAssetUrl:NSUrl toAlbum:NSString withCompletionBlock:^(NSError *error)]來了從我發現的類別here

我真的在這裏找到正確的URL嗎?還是我錯誤地使用了dataWithContentsOfURL

+0

所以我找到[這裏](http://stackoverflow.com/questions/5187251/ios-select-a-gif - 從照片庫轉換爲nsdata使用在多部分),同時戳動。它似乎添加了太多的代碼,我很關心內存,因爲我在上傳照片時會出現一個NSData和bytea對象。能有更直接的方法嗎?.. –

回答

1

唯一的辦法是您可以使用ALAsset URL從照片庫中檢索UIImage並將其轉換爲NSData。

添加ALAssetLibrary

導入ALAssetLibrary頭文件

-(void)uploadItemAtIndex:(NSUInteger)index 
     withProgressBlock:(progressBlock)pBlock 
      withExitBlock:(exitBlock)eBlock 
{ 

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
    { 
     ALAssetRepresentation *rep; 
     if([myasset defaultRepresentation] == nil) { 
      return; 
     } else { 
      rep = [myasset defaultRepresentation]; 
     } 
     CGImageRef iref = [rep fullResolutionImage]; 

     dispatch_sync(dispatch_get_main_queue(), ^{ 


      UIImage *myImage = [UIImage imageWithCGImage:iref];    
      NSData *data = //convert the myImage to NSData 

      BCard *item = [uploadQueue objectAtIndex:index]; 
      NSURL *url = [NSURL URLWithString:@"http://192.168.0.116:8080"]; 
      AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url]; 
      numberedName = numberedName +1; 
      NSString *name = [NSString stringWithFormat:@"%d",numberedName]; 



      //upload data using AFNetworking here 


     });//end block 


    }; 
    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
    { 
     NSLog(@"Cant get image - %@",[myerror localizedDescription]); 
    }; 

    NSURL *asseturl = 
     [NSURL URLWithString:[self.photoPath objectAtIndex:[arrayIndex intValue] ]]; 
    //using ARC , you have to declare ALAssetsLibrary as member variable 
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 

    [assetslibrary assetForURL:assetURL 
        resultBlock:resultblock 
        failureBlock:failureblock]; 

}