0

我正在尝试将我的应用中的照片上传到网络服务中。

我试图创建的流程如下:

  1. 用户用相机拍照
  2. 照片保存到自定义相册下的相机胶卷
  3. 已保存照片的 URL 已提供给我的商店
  4. 商店尝试将照片上传到 Web 服务。

我正在尝试使用NSData *data = [NSData dataWithContentsOfURL:[item assetURL]]where item 是包含相关 URL 的模型。但是,即使它产生了 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)]来自我在这里找到的类别。

我真的在这里得到了正确的 URL 还是我使用dataWithContentsOfURL不正确?

4

1 回答 1

1

唯一的方法是您可以使用 ALAsset URL 从 Photo-Library 检索 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]; 

}
于 2012-11-23T05:16:19.890 回答