选择的图像 xcode 的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13512260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:10:08  来源:igfitidea点击:

name of the picked image xcode

objective-ciosxcodeuiimagepickercontroller

提问by Assassin

I want to get the name of the image picked from the library or newly clicked in -

我想获取从库中选取或新点击的图像的名称 -

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

I am using this code:

我正在使用此代码:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        AppDelegate *del = [[UIApplication sharedApplication]delegate];
        [self dismissModalViewControllerAnimated:YES];
        image = [info objectForKey:UIImagePickerControllerOriginalImage];

        NSLog(@"imagekbgfg=====>%@",image);
        [self.imageView setImage:image];

        imgName = [info objectForKey:UIImagePickerControllerOriginalImage];

        del.mainImgName = imgName;
        del.imageData = UIImageJPEGRepresentation(image, 1.0);
        del.imageApp = image;
    }

    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
       image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
}

How can I do this?

我怎样才能做到这一点?

回答by sunkehappy

Using the code bellow you will get path like:assets-library://asset/asset.JPG?id=79450962-C0FD-484C-808B-83E045F40972&ext=JPGwhen your picking image from photo album. This path is valid. It contains the asset ID and asset type.

使用下面的代码,您将获得如下路径:assets-library://asset/asset.JPG?id=79450962-C0FD-484C-808B-83E045F40972&ext=JPG当您从相册中选择图像时。此路径有效。它包含资产 ID 和资产类型。

// Firstly get the picked image's file URL.
NSURL *imageFileURL = [info objectForKey:UIImagePickerControllerReferenceURL];

// Then get the file name.
NSString *imageName = [imageFileURL lastPathComponent];
NSLog(@"image name is %@", imageName);

If you want to get image user picked again using the path you get using UIImagePickerController.(Here maybe you want to save the path so that you can read that image back without asking user to pick that image again) You will need the ALAssetsLibraryotherwise you don't need it.

如果您想使用您使用的路径再次选择图像用户UIImagePickerController。(这里也许您想保存路径,以便您可以在不要求用户再次选择该图像的情况下读取该图像),ALAssetsLibrary否则您将不需要'不需要。

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block UIImage *returnValue = nil;
[library assetForURL:imageFileURL resultBlock:^(ALAsset *asset) {
    returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
} failureBlock:^(NSError *error) {
    NSLog(@"error : %@", error);
}];

回答by Nicolinux

Take a look at the UIImagePickerControllerDelegate Protocol. The string UIImagePickerControllerReferenceURLcontains an URL of your file. You could use it to construct the filename from it.

看看 UIImagePickerControllerDelegate 协议。字符串UIImagePickerControllerReferenceURL包含文件的 URL。您可以使用它来构造文件名。

Other than that there seems to be no built-in way to get the file name from an UIImage or UIImageView object.

除此之外,似乎没有从 UIImage 或 UIImageView 对象获取文件名的内置方法。

Reference: Stackoverflow answer

参考: Stackoverflow 答案

回答by Jashu

You can get the file name using below code:

您可以使用以下代码获取文件名:

NSURL *assetURL = [imageDic objectForKey:UIImagePickerControllerReferenceURL];

 __block NSString *originalFileName = nil;

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
 [library assetForURL:assetURL resultBlock:^(ALAsset *asset){
             originalFileName = [[asset defaultRepresentation] filename];
}

originalFileName give the original file name of resource.

originalFileName 给出资源的原始文件名。