1

I have a UIImageView that diaplays an image. 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.

I know how to save into plist and all. The problem is only that I don't know how to get the file path of the image being displayed in the UIImageView.

I did homework and found few articles but im still confused. Any help will be appreciated.

pnuts
  • 58,317
  • 11
  • 87
  • 139
SHA
  • 15
  • 2
  • 6

3 Answers3

1

I dont think it is possible to get the image path from a UIImage, if by path you mean the original location on disk, then i dont think that you can do it

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • are you getting image from photo album or other source ? – Mani Jun 25 '12 at 07:11
  • Yes I am getting image from my album in Iphone Simulator. – SHA Jun 25 '12 at 07:13
  • can you put that code here where you are adding image on imageview ?? – Abhishek Jun 25 '12 at 07:20
  • [code] -(IBAction) getPhoto:(id) sender { UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = self; if((UIButton *) sender == choosePhotoBtn) // select from album { picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } else { picker.sourceType = UIImagePickerControllerSourceTypeCamera; } [self presentModalViewController:picker animated:YES]; } – SHA Jun 25 '12 at 07:44
0

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

 #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; your Imageview.imageFileName=yourImageFilepath;

to get the yourImageFilepath add "AssetsLibrary.framework" to your framework folder.Then add #import "AssetsLibrary/AssetsLibrary.h"; to the header file of your class.

then add the following code in "didFinishPickingMediaWithInfo" method

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.

Madhumitha
  • 3,794
  • 8
  • 30
  • 45
  • Madhumitha I did the same. But its not working. this is what I have done. `#import @interface Imager : UIImageView { NSString* imageFileName; }` @end `#import "Imager.h" @implementation Imager -(void) setImage:(NSString*)fileName { imageFileName = fileName; [super setImage:fileName]; } @end` Its giving warning at [super setImage:fileName]; that I can not pass String to setImage function – SHA Jun 25 '12 at 07:52
  • you can not use setImage because it take UIImage parameter not of NSString. To achieve Madhumitha solution you have to make property of imageFileName. Hope this will help. – fibnochi Jun 25 '12 at 08:11
  • I did try to assign imageview.imagefilename to a string variable Where im assigning my imageview.image , but imageFileName just doesn't show up in the recommendations :( Please help. and Thanks for all the help! Really appreciate IT. – SHA Jun 25 '12 at 09:38
  • Create your UIImageView as Imager *image = [[Imager alloc] init];image.imageFileName = yourimagepath; – Madhumitha Jun 25 '12 at 09:57
  • Sorry Madhumati. my next question might be stupid but im really confused. Where actually are we getting this "yourimagefilepath" .. I mean, where are we getting the actual path of our image ? – SHA Jun 25 '12 at 10:35
0

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. And you can save the image names in the plist. Using the names here you can do the further stuff easily.

Melbourne
  • 531
  • 3
  • 24