如何从iOS中的iPhone照片库中选择图像名称和图像

时间:2016-06-29 12:11:37

标签: ios objective-c iphone uiimagepickercontroller

我正在从iPhone照片库中选择图像或以其他方式从相机胶卷中选择图像。我毫无问题地完成了这件事。但我需要检索从库中选择图像名称。但是我无法从图库中选择图像名称。我的要求是我想从照片库中选择图像和图像名称并发送聊天墙。

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

1 个答案:

答案 0 :(得分:1)

使用“照片”框架并导入Photos / Photos.h

#import "Photos/Photos.h"

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

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

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