如何从iOS中的iPhone照片库中选择图像名称和图像 [英] How to pick the image name and image from iPhone photo library in iOS

查看:180
本文介绍了如何从iOS中的iPhone照片库中选择图像名称和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从iPhone照片库或以其他方式从相机胶卷中选择图像.我已经做到了,没有任何问题.但是我需要从库中检索选择图像的名称.但我无法从图片库中选择图片名称.我的要求是我想从相册中选择图像和图像名称,然后发送聊天墙.

I'm doing a pick a images from iPhone photo library or in other way from camera roll. I've done that without any problem. But I need to retrieve the picking the image name from the library. but I cant able to pick the image name from gallery. My requirement is I want to pick the image and image name from photo gallery and send the chat wall.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];

    UIImage *image =[[UIImage alloc] init];

    image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

//Here I am able to get the image path like this: assets-library://asset/asset.JPG?id=9F983DBA-EC35-42B8-8773-B597CF782EDD&ext=JPG

    imageName = [imagePath lastPathComponent];

    NSData *imageData;

    NSString *extensionOFImage =[imageName substringFromIndex:[imageName rangeOfString:@"."].location+1 ];

    if ([extensionOFImage isEqualToString:@"jpg"])
    {
        imageData =UIImagePNGRepresentation(image);
    }
    else
    {
        imageData = UIImageJPEGRepresentation(image, 1.0);
    }

    int imageSize=imageData.length/1024;

    NSLog(@"imageSize--->%d", imageSize);

    if (imageName!=nil) {

        NSLog(@"imageName--->%@",imageName);

//here the imageName is showing like this "asset.jpg"..

    }
    else
    {
        NSLog(@"no image name found");
    }
}

-(void)sendMessage:(NSString *)messageTxt {

    str=@"Ryan";

    mesText=messageTxt;

    senderImgName=[UIImage imageNamed:imageName];

//here the imageName is showing like this = asset.jpg

//but senderImgName= null.

    NSString *str1=imageName;

    NSLog(@"sender image name is:%@",senderImgName);

    imgData=[NSData dataWithData:UIImageJPEGRepresentation(senderImgName, 0)];

    finalImagePath=[imgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

    NSLog(@"Final image path is:%@", finalImagePath);
}

推荐答案

使用Photos框架并导入Photos/Photos.h

Use the Photos framework and import Photos/Photos.h

#import "Photos/Photos.h"

在您的imagePickerController函数中,添加以下内容以从照片库中获取所选图像的文件名:

In your imagePickerController function add the following to get the filename of the selected image from the Photo library:

NSURL *imagePath = [editingInfo objectForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[imagePath] options:nil];
NSString *filename = [[result firstObject] filename];
NSLog(@"[filename ] %@", filename );

结果是这样的:[filename] IMG_0012.JPG

The result is like this: [filename ] IMG_0012.JPG

这篇关于如何从iOS中的iPhone照片库中选择图像名称和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆