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...
Related
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];
}
I am taking the screenshot of my last viewed Screen, by the following code
//Image Code
UIImage *nextImage;
CGImageRef imgRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
nextImage = [UIImage imageWithCGImage:imgRef];
CGImageRelease(imgRef);
CGContextRelease(UIGraphicsGetCurrentContext());
When i displayed the the image in the console i got the O/P like: UIImage: 0x929c760
The Problem lies here for me, When converting that UIImage to NSData i am getting null.
The things i have tried are
//tried
NSData *data = UIImagePNGRepresentation(nextImage);
NSData *data2 = UIImageJPEGRepresentation(nextImage, 1.0);
Hi everyone i have got the solution to my Question as given by the link by #NP Compete
//getting screenshot i haven't used any UIImageview so my code goes as following
-(UIImage*)doImageOperation
{
UIImage *image = [[UIImage alloc]init];
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(#"image in cg %#",image);
[self saveImage:image];
return image;
}
// Saved into Documents Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString:#"test.png"]];
data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
NSData *data = UIImagePNGRepresentation(image1);
NSLog(#"data %#",data);
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];
}
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.
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.