How to save uiimage in a directory on a given nsurl? - ios

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];

Related

how to fix file writing issue into documents directory?

I have implemented the logic of writing files into document directory.
NSData *pngData = UIImagePNGRepresentation([UIImage imageNamed:#"logo_icon_login.png"]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"];
if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath])
{
[pngData writeToFile:filePath atomically:YES];
}
else
{
NSLog(#"Not writable.");
}
Now the problem is in some devices it says Not writable. Is there any permission should I need before writing files in the documents directory?
Create a folder namely images and than put the image inside. It could be root directory issue.
Try this:
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"images/image.png"];

How to write data to my App document after take a photo from Camera?

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];

xcode5: how can I save and load an image?

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

Why can't I save files to my iOS apps /Documents directory?

I am writing an app that can access the iOS root system, the user should be able to save files to his document directory. I am using this code to save the file to the document directory.
For Text:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[self.filePath writeToFile:[NSString stringWithFormat:#"%#/%#", documentsDirectory, [self.filePath lastPathComponent]]
atomically:YES
encoding:NSUTF8StringEncoding error:nil];
For other files:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[self.filePath writeToFile:[NSString stringWithFormat:#"%#/%#", documentsDirectory,[self.filePath lastPathComponent]]
atomically:YES];
My problem is I can save .txt files, but not other files, If I save for example .plist files with the save text methods, the contact is replaced by the directory path of the file. When I save a picture, or any other file it isn't readable. Is there a good method to save files without destroying them?
You're calling [self.filePath writeToFile:], thus writing the contents of the filePath variable to a file.
You should do something like:
[contents writeToFile:...]
Here you have an example of saving the image:
assuming you get the content of the image into NSData object:
NSData *pngData = UIImagePNGRepresentation(image);
then write it to a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
Have a look into these questions for more detailed explanations:
Write a file on iOS
Save An Image To Application Documents Folder From UIView On IOS

iOS : Saving images to app folder and accessing it

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.

Resources