2012-11-09 118 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]; 

}