I'm developing an app, where I can apply filters on an image. I want to create an undo button which resets the original image in the imageView.
The solution is that I just save the original image in it's own UIImage object before I apply any filters to it. This way I can just go back to that in my undo method.
Does somebody know how I can do this?
Take a look at the docs here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html
You can do what you want with UIImagePNGRepresentation().
Then to re-read: imageWithContentsOfFile:
Get Image from disk . Suppose image stored in document directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourImgPath = [documentsDirectory stringByAppendingPathComponent:#"1.png"];
UIImage *originalImage = [UIImage imageWithContentsOfFile:yourImgPath];
Create tempImage from originalImage
UIImage *tempImage = originalImage;
Appy filters on tempImage
EDIT : How to save then below is method to save in document directory.
- (BOOL)saveImageInDocDir:(UIImage *)image withImagename:(NSString *)strImgName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strImgName];
NSData *imageData = UIImagePNGRepresentation(image);
BOOL isSaved = [imageData writeToFile:savedImagePath atomically:YES];
return isSaved;
}
You can also use NSTempraryDirectory to save images as its safe from cloud backup
Related
In iOS 8, I am writing UIImages on the device using the code
NSString *documentsDirectory = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *pathString = [NSString stringWithFormat:#"%#/%#%ld",documentsDirectory, #"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:#"PatientsId"]];
NSData *imageData = UIImageJPEGRepresentation(image, 40);////I have replaced it with UIImagePNGRepresentation as well
[imageData writeToFile: pathString atomically:YES];
The write to file is successful.
If I try to recover the image using the function,
NSArray *documentDirectories =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
// Get one and only document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
NSString *pathString =[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#%ld", #"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:#"PatientsId"]]] ;
UIImage *image = [UIImage imageWithContentsOfFile: pathString];
The image is recovered.
However, if I restart the device, the images are not there anymore. The function [UIImage imageWithContentsOfFile: pathString] returns nil and if I use NSData *myData = [[NSData alloc] initWithContentsOfFile: pathString];, the myData is nil as well.
This procedure was working perfectly fine prior to iOS 8, however, it isn't working properly anymore. Can someone please guide me what is wrong with this?
It's possible to save the shooting pictures into the photo library through apply for writeImageDataToSavedPhotosAlbum: after took pictures by camera.
But I don't want to save the picture into the photo library and wanna to save them into my owned APP catalog. Currently, I can get the NSdata and Metadata.
Is there anybody could advise me what I should do? Really appreciated!!
To save the NSdata to your own app documents folder, use the below code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirPath = [paths objectAtIndex:0];
NSString *filePath = [documentDirPath stringByAppendingPathComponent:#"ImageName.png"];
[yourNSdata writeToFile:filePath atomically:YES];
You can read it after using the below code,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:filePath];
NSData *myImageData = [NSData dataWithContentsOfFile:imagePath];
UIImage *savedImage = [UIImage imageWithData:myImageData];
I have Five buttons.On each button action.I have open gallery and choose an image.But the problem is on each button action same path is coming.
UIImage* image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
NSData* imageData = UIImagePNGRepresentation(image);
NSString * imageName = #"Myimage.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(#"fullPathToFile=%#",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
Set global variable initially int countVal = 1;
NSString * imageName = [NSString stringWithformat:#"%d.png",countVal]; // Imgae_name set here
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(#"fullPathToFile=%#",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
count +=1; // Increment count here
I may be missing something, but it looks like you're using the same name for the image each time: Myimage.png. Every time the image will be saved inside the documents directory with that same name. Maybe add a tag to each button and use that to distinguish each of the 5 different images?
May be it will helpful for u
[picker dismissModalViewControllerAnimated:YES];
imageView.image= [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(imageView.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:png];
[webData writeToFile:localFilePath atomically:YES];
NSLog(#"localFilePath.%#",localFilePath);
UIImage *image = [UIImage imageWithContentsOfFile:localFilePath];
OR
Use this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
imageView.image= [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:savedImagePath atomically:NO];
NSLog(#"localFilePath.%#",localFilePath);
If you still get it empty then try by checking if NSData is created properly
NSLog(#"webData.%#",webData);
OR
Try this: I hope defnetly it will be be helpful to you
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
//you can use UIImagePickerControllerOriginalImage for the original image
//Now, save the image to your apps temp folder,
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:#"upload-image.tmp"];
NSData *imageData = UIImagePNGRepresentation(img);
//you can also use UIImageJPEGRepresentation(img,1); for jpegs
[imageData writeToFile:path atomically:YES];
//now call your method
[self uploadMyImageToTheWebFromPath:path];
}
Try to use below code which just edited to you answer
The problem is only with saving image name,try to save it with different name
The below code will help you in saving image with diffrent name
note: put the tag value to diffrent button(5 buttons) and call bello method in its target
-(void)saveImage:(UIButton*)sender{
UIImage* image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
NSData* imageData = UIImagePNGRepresentation(image);
NSString * imageName =[NSString stringWithFormat:#"Myimage%d.png",sender.tag];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(#"fullPathToFile=%#",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
}
by this you can save image with different name
I have fetched UIImage from photos library in ipad using UIImagePickerController. Now I want to save that image in a directory that is associated with my app and contains already added images. I have also got the url of the directory.
For example : file://localhost/Users/administrator/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/31816EF9-AE1E-40DF-93C4-C67A82E4B85B/Documents/7884/Images/
How can I save uiimage in the specified directory?
Use this code to save it into your Document Directory.
NSData *pngData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"];
[pngData writeToFile:filePath atomically:YES];
I am trying to take an image and place that image to imageView, I am able to do this. But I am not able to save this to device, I want my photo to be saved in my application folder and want to access it from another view.
studentImage is my UIImageView element where I am able to set the image taken from camera.
Here is the code I am working with :
NSString* format = #".png";
NSString* imagename = [NSString stringWithFormat:#"%#%#",nickname,format];
UIImage *img = [self.studentImage image];
NSData *pngData = UIImagePNGRepresentation(img);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:imagename];
[pngData writeToFile:filePath atomically:YES];
This statement is returning null : [pngData writeToFile:filePath atomically:YES];
It would be great, if some one can tell me how to access that image from another view.