获取从相机/照片库中拍摄的图像的名称

时间:2012-08-25 00:17:45

标签: iphone objective-c xcode cocoa-touch

我在获取从照片库或相机中拍摄的图像名称时遇到问题。我已成功选择了图像,但我想使用MFMailComposeViewController将拾取的图像作为电子邮件附件发送。问题是MFMailComposeViewController需要一个特定的图像名称。我使用以下代码来获取图像名称,它总是返回“assest.JPG”并且无法使用MFMailComposeViewController

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
[imageView setImage:image];
image_ = image;
image_ = [UIImage imageNamed:@"file.jpg"];
NSURL *imagePath = [editingInfo objectForKey:@"UIImagePickerControllerReferenceURL"];
imageName = [imagePath lastPathComponent];

NSLog(@"%@",imageName);
[self dismissModalViewControllerAnimated:YES];
if (CAMORLIB == 1) {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}}

还有其他方法可以解决这个问题吗?

谢谢,

穆罕默德

2 个答案:

答案 0 :(得分:0)

您的代码在这里:

image_ = image;
image_ = [UIImage imageNamed:@"file.jpg"];

没有意义,因为它分配给image_两次。 UIImage方法-imageNamed:(NSString*)name尝试从磁盘加载名为file.jpg的文件。

MFMailComposeViewController无效的原因是上述代码导致image_nil,因为[UIImage imageNamed:@"filename"]找不到名为该文件的文件(以及结果返回nil

附加邮件

将图像附加到MFMailComposeViewController时,您可以告诉它想要图像的文件名,以及图像的数据:

NSData* myData = UIImageJPEGRepresentation(image, .70f);
[picker addAttachmentData:myData mimeType:@"image/jpg" fileName:@"desiredfilename.jpg"];

此处,imageUIImage@"desiredfilename.jpg"是您希望图片在电子邮件中显示的文件名。

答案 1 :(得分:0)

从资产库中提取图像只会为您提供该项目的guid网址。

资产库中没有真正的名称结构。照片库有一些属性,但它们如下

ALAssetPropertyType
ALAssetPropertyLocation
ALAssetPropertyDuration
ALAssetPropertyOrientation
ALAssetPropertyDate

所有这些都可以在AssetsLibrary.framework\ALAsset.h

中找到

为我从那里收集的图像命名,我通常会把

[NSString stringWithFormat:"Image%@.png",imageNumber]

当我添加图像时图像编号递增。

希望有所帮助

相关问题