0

我正在使用 imagePickerController 从图库和使用设备相机拍摄照片。

我的问题是,如果从图库中选择图像,那么我可以获得它的“文件名”,如果我使用相机获取图像,那么它的“文件名”等于“(null)”。

我需要“文件名”,因为我正在将图片上传到服务器,而我正在使用的 .php 文件需要上传图片的文件名。

这是我正在使用的代码

- (IBAction)addPicture:(id)sender
{
    [[[UIActionSheet alloc] initWithTitle:@"Select from:"
                                 delegate:self
                        cancelButtonTitle:@"Cancel"
                   destructiveButtonTitle:nil
                        otherButtonTitles:@"Gallery", @"Camera", nil] showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *)kUTTypeImage,  nil];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:NULL];
    }
    else if (buttonIndex == 1)
    {
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [[[UIAlertView alloc] initWithTitle:@"Error"
                                        message:@"Device has no camera."
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles: nil] show];
        }
        else
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *)kUTTypeImage,  nil];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;

            [self presentViewController:picker animated:YES completion:NULL];
        }
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *chosenImage = [info[UIImagePickerControllerEditedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];

    [_selectedImageView setImage:chosenImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *representation = [myasset defaultRepresentation];
        NSString * fileName = [representation filename];
        selectedImageName = [NSString stringWithFormat:@"%@", fileName];
        NSLog(@"%@", selectedImageName);
    };

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:imageURL
                   resultBlock:resultblock
                  failureBlock:nil];
}

这是我得到的输出,来自画廊的图片:

IMG_3383.PNG

来自相机的图像:

(null)

任何知道如何获取从设备相机拍摄的图像的“文件名”的人?

4

0 回答 0