【问题标题】:iOS: I am able to save image but new image name returns niliOS:我可以保存图像但新图像名称返回 nil
【发布时间】:2014-01-24 14:18:04
【问题描述】:

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

- (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];
}

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

【问题讨论】:

    标签: ios uiimagepickercontroller


    【解决方案1】:

    我仍然得到 nil,但我通过将图像保存到临时目录并使用此路径进行上传解决了我的问题:

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

    【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2019-03-19
    • 1970-01-01
    • 2019-04-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多