iOS:我可以保存图像,但新图像名称返回nil

时间:2014-01-24 14:18:05

标签: ios uiimagepickercontroller

您好我正在尝试构建和界面,它为用户提供了选择现有图像或从相机中获取新图像的选项。选择现有图像工作正常,我可以获取图像文件名。但是当我尝试拍摄新图像时,我可以将图像保存到库但无法获取其文件名。它返回零。这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"Dismissing camera ui...");
    [self.cameraUI dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"Getting media url...");
    NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];

if(mediaURL) {
    NSLog(@"This is a video = %@", mediaURL);
} else {
    NSLog(@"This is a photo...");

    NSLog(@"Getting media type...");
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    NSLog(@"Selected mediaType      : %@", mediaType);

    UIImage *originalImage;

    if (self.source == UIImagePickerControllerSourceTypeCamera) {
        // Handle a still image capture
        if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
            originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

            NSLog(@"Saving new image...");

            if (self.source != UIImagePickerControllerSourceTypePhotoLibrary) {
                UIImageWriteToSavedPhotosAlbum(originalImage, nil, nil , nil);
            }
        } else {
            // Handle a movie capture
        }
    }

    NSLog(@"Getting reference url...");
    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibrary *assetLibrary = [ALAssetsLibrary new];

    [assetLibrary assetForURL:referenceURL
                  resultBlock:^(ALAsset *asset) {
                      ALAssetRepresentation *assetRep = [asset defaultRepresentation];
                      NSString *filename = [assetRep filename];
                      NSLog(@"File name = %@", filename);
                      if(self.selectedMediaNames == nil)
                          self.selectedMediaNames = [[NSMutableArray alloc] init];

                      [self.selectedMediaNames addObject:filename];
                      [self.tableView reloadData];
                      [self.activitIndicator stopAnimating];
                      [self.activitIndicator setHidden:true];
                  }

                 failureBlock:^(NSError *error) {
                     NSLog(@"%@", error);
                 }];
}

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

我的目标是使用此图片名称将图片上传到服务器。

1 个答案:

答案 0 :(得分:0)

我仍然没有问题但我通过将图像保存到临时目录并使用此路径上传来解决我的问题:

NSString *path = [NSTemporaryDirectory()
                      stringByAppendingPathComponent:@"upload-image.tmp"];
    NSData *imageData = UIImageJPEGRepresentation(originalImage, 1.0);
    [imageData writeToFile:path atomically:YES];