简体   繁体   English

如何从UIImageview获取图像文件路径

[英]How to get image file path from UIImageview

I have a UIImageView that diaplays an image. 我有一个UIImageView可以播放图像。 Then I have a button titled 'save' . 然后,我有一个名为“保存”的按钮。 What I want to do is that when I click the button, it should save the image file name into a plist for further retrieval and stuff. 我想做的是,当我单击按钮时,它应该将图像文件名保存到plist以供进一步检索和使用。

I know how to save into plist and all. 我知道如何保存到plist和所有。 The problem is only that I don't know how to get the file path of the image being displayed in the UIImageView . 问题仅在于我不知道如何获取UIImageView中显示的图像的文件路径。

I did homework and found few articles but im still confused. 我做了家庭作业,发现很少的文章,但我仍然感到困惑。 Any help will be appreciated. 任何帮助将不胜感激。

我不认为有可能从UIImage获取图像路径,如果按路径表示磁盘上的原始位置,那么我不认为可以做到这一点

Create UIImageview as CustomImageview class with NSString variable.Then while showing that image,store the image file path in that string variable.Then you can get it where ever you want with the use of that string.Use the following code 使用NSString变量将UIImageview创建为CustomImageview类。然后在显示该图像时,将图像文件路径存储在该字符串变量中。然后可以使用该字符串在任何位置获取它。使用以下代码

 #import <UIKit/UIKit.h> 
 @interface Imager : UIImageView  
 { 
 NSString* imageFileName; @property(nonatomic,retain)NSString* imageFileName;
 }
 @end

 #import "Imager.h" 
 @implementation 
 @synthesize imageFileName

and the assign the filepathname where you are assigning the image like your Imageview.image = UIImage; 并在分配图像的位置分配文件路径名,例如Imageview.image = UIImage; your Imageview.imageFileName=yourImageFilepath; 您的Imageview.imageFileName=yourImageFilepath;

to get the yourImageFilepath add "AssetsLibrary.framework" to your framework folder.Then add #import "AssetsLibrary/AssetsLibrary.h"; 要获取yourImageFilepath,请在框架文件夹中添加“ AssetsLibrary.framework”。然后添加#import“ AssetsLibrary / AssetsLibrary.h”; to the header file of your class. 到您的类的头文件。

then add the following code in "didFinishPickingMediaWithInfo" method 然后在“ didFinishPickingMediaWithInfo”方法中添加以下代码

UIImage *viewImage = yourImage;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];  
[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:   (ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error)
{  
if (error) 
{  
    NSLog(@"error");  
} 
else 
{  
    NSLog(@"url %@", assetURL);  
}  
}];  
[library release];

It will return a url.that is your yourImageFilepath. 它将返回一个URL。即您的imageFilepath。

The best way to do this is to Save the image in document folder with name image1, image2, etc or the exact name of the image if you know it. 最好的方法是将图像保存在文档文件夹中,其名称为image1,image2等,或者如果您知道图像的确切名称。 And you can save the image names in the plist. 您可以将图像名称保存在plist中。 Using the names here you can do the further stuff easily. 使用此处的名称,您可以轻松地进行其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM