Adding Image to plist [duplicate] - iphone

This question already has answers here:
Storing image in plist
(4 answers)
Closed 9 years ago.
I'm making an app where I take picture using camera and want to save those picture on plist so that i can use in future. I also want to retrive the saved images from plist and display them on imageview. There are multiple images.Please anyone can help me with this
Here is my code which I'm using for adding to plist
imageArray addObject:[NSString stringWithFormat:#"%#",imageView.image]];
[imageArray writeToFile:plistPath atomically:YES];
But when I'm trying to retrive image and set it on imageview I cant see image.
Thanks.

You can't save like this method to plist. The supported format are NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber. You can convert your UIImage into NSData and can write in plist.
To convert an image into NSData
NSData *data = UIImagePNGRepresentation(imageview.image);
[imageArray addObject:data];
[imageArray writeToFile:plistPath atomically:YES];
and also NSLog your file path to find where yoyr plist is saved.
Hope this helps !!!

see this one ,
For saving :
- (IBAction)saveImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
For retrieving:
- (IBAction)getImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
}

Related

iPhone GLImageProcessing

in one of the example of GLImageProcessing i m just changing the way of getting image but app get crashed help me please i have change the way in such a manner
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
// UIImage *image = image; // imageView is my image from camera
UIImage* img11 = [UIImage imageNamed:#"image1.jpg"];
NSData *imageData = UIImagePNGRepresentation(img11);
[imageData writeToFile:savedImagePath atomically:NO];
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
NSString *getImagePath1 = [documentsDirectory1 stringByAppendingPathComponent:#"savedImage.png"];
UIImage *imgGet = [UIImage imageWithContentsOfFile:getImagePath1];
CGImageRef CGImage = imgGet.CGImage;
However earlier way was ....
CGImageRef CGImage = [UIImage imageNamed:[NSString stringWithUTF8String:name]].CGImage;
I am not much sure but UIImagePNGRepresentation gives internal data so read it back as data and then convert it into image using following lines.
NSData *data1 = [NSData dataWithContentsOfFile:#"filePath"];
UIImage *uiImage = [[UIImage alloc] initWithData:data1];"
Please consider following block also. Put check for nil to avoid unexpected execution.
Return Value
An autoreleased data object containing the PNG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
Check if above is helpful...

How do I save an image to the sandbox - iPhone SDK

I have pulled an image out of the UIImagePicker thingy, and I would like to temporarily save it to the sandbox environment, in either the documents folder, or some other similar equivalent. However, I do not know how to do this, please could someone help me? Thanks
At the moment the image is a UIImage
Saving an image:
-(void)saveImage:(UIImage*)image{
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/someImageName.png"];
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
}
Loading an image
-(UIImage)loadImage{
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/someImageName.png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
}
-(void)SaveImage
{
NSData *data;
NSArray *paths;
NSString *documentsDirectory,*imagePath ;
UIImage *image=[UIImage imageNamed:#"public.png"];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"public.png"]];
data = UIImagePNGRepresentation(image);
[data writeToFile:imagePath atomically:YES];
}

Storing images to the iphone

Is it possible to save images captured by the camera on the iphone to a custom directory or somewhere on the iphone rather than saving to the camera roll?
I would like to save images to the device and then have access to the at a later stage, rather than reading from the camera roll using ALAssets.
Is this possible?
Yes you can save them to your apps' documents folder. Here is an example:
// Create path
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/ImageName.png"];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
To retrieve it:
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
To list all files in a directory:
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"]];
for (NSString *s in fileList){
NSLog(s);
}
Use this to write
// imageData = NSData object
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [docsPath stringByAppendingPathComponent:#"anyFolderName"];
NSString *imageCachePath = [imageCacheDirPath stringByAppendingPathComponent:#"image.jpg"];
[imageData writeToFile:imageCachePath atomically:YES];
And the following snippet to read the data again:
// Get path of image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [docsPath stringByAppendingPathComponent:#"anyFolderName"];
NSString *imageCachePath = [imageCacheDirPath stringByAppendingPathComponent:#"image.jpg"];
NSData *imageData = [NSData dataWithContentsOfFile:imageCachePath];
Note that in this example the image is stored in a subfolder of your "Documents" folder and this folder is called "anyFolderName". The variable docsPath contains the Path to the "Documents" folder of your dir.
Note that this is only the code I use, its from iOS 3.0 times, there might be better ways to store an image to the Documents folder of your app.

How to store image in big size in local?

I am storing an image locally but my image is store in small size. i want to store this image in orignal size how can i do this ?
my code for store image is like this ..
NSString *myurl = [[NSString alloc]init];
myurl =[data valueForKey:#"large_image"];
NSData *mydata = [[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myurl]] retain];
UIImage *myimage = [[[UIImage alloc] initWithData:mydata] retain];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *imageName = [NSString stringWithFormat:#"image.png",documentsDirectory];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:Imagename];
[imageData writeToFile:fullPathToFile atomically:YES];
this is stored image
and this is orignal image
i am doing wrong in code. this is the reason. why this happend.
may be it is because of leak ok knowledge that time. :)
other wise all things is correct only.

How to create an Image from byte array

M stuck M trying to create an UIImage from a byte array which i get from a webservice.It comes embedded in a XML.I can parse the XML and get the byte array as a string.Then I convert the byte array (which is in NSString) to NSData. This is the code for that:-
Image_Content = currentElementValue;
NSData* aDataImage;
aDataImage = [Image_Content dataUsingEncoding: NSASCIIStringEncoding];
UIImage *Image_From_Array = [[UIImage alloc] initWithData:aData];
Can I save this file in certain format as jpg or png?
I want to save this image as a file so i can later use it in the HTML (this image is linked in the HTML).
Can any body help?
Thanks.
To write your data to the filesystem, use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* fileName = [NSString stringWithFormat:#"%#/%#", documentsDirectory, aFileName];
BOOL writeSuccess = [imageData writeToFile:fileName atomically:NO];
Then to reload it later do the same:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* fileName = [NSString stringWithFormat:#"%#%#", documentsDirectory, aFileName];
image = [UIImage imageWithContentsOfFile:fileName];
You can also change your complression type by creating your image and generating the imageData using UIImageJPEGRepresentation and UIImagePNGRepresentation to generate the data you write to the filesystem.
Have a look at the functions UIImageJPEGRepresentation and UIImagePNGRepresentation.