How to get saved image file path in iOS - iphone

Hi I am developing an app in which there is an option to pick the photos from iOS photo gallery. I have to upload the selected photos into the server. To upload the images i am using Kaltura SDK so i am in the situation not able to directly send the UIImage to server. i need to send saved image file path.
For that I found an solution, That is saving the selected photos in a particular file path then i will use that file path. But the problem is i do not wish to save photos again in phone memory because those photos are already stored in a particular path.
So Is there any other option to retrive the saved image file path without saving it again?

Try this one:
- (void)saveImage: (UIImage*)image
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"test.png" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"test.png" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
[self sendAction:path];
return image;
}

Related

Save uiimage with custom format

I want to save an image with ".wai" extension, for send it trough whatsapp.h
ow can I save the image with this format?
The whatsapp site don't have a sample code, for more information this is the link (propbably visiting this link you understand my problem)
http://www.whatsapp.com/faq/en/iphone/23559013
NSData *pngData = UIImagePNGRepresentation(drawimg.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
filePath = [documentsPath stringByAppendingPathComponent:#"edited.wai"]; //Add the file name
[pngData writeToFile:filePath atomically:YES];

How to save pdf locally from image view

How to save the pdf locally
I have loaded the pdf doc into imageView. Now i need to save into locally into iPhone device.
Any one advice me how to save the pdf file from image view
#All
Thanks in advance
For saving the pdf in document directory use below code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:`ADD URL OF PDF DATA`]];
[thedata writeToFile:localFilePath atomically:YES];
For retrieving the pdf use below code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
if ( [[NSFileManager defaultManager] fileExistsAtPath:localFilePath] ) {
NSData *data = [NSData dataWithContentsOfFile:localFilePath];
}

iOS Saving Media Picker Selections

I am trying to create an app that saves the user's media selections from a Media Picker to be reused and avoid having to re-pick the items . Is there a way to do this with the Media Picker or any example code for this?
The only way to do this is to save the media selection in the app's sandbox i.e. Documents directory for re-use. Because, you can't access the media without user intervention i.e. you've to present media picker each time whenever you wanna pick something from the gallery. You can use something like this to save an image in app's doc directory:
+(BOOL)saveImageInDocumentsDirectory:(UIImage*)image WithName:(NSString*)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:name];
NSData *imageData = UIImagePNGRepresentation(image);
return [imageData writeToFile:savedImagePath atomically:NO];
}
And to retrieve the images:
+(UIImage*)getImageFromDocumentsDirectoryWithName:(NSString*)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:name];
return [[[UIImage alloc] initWithContentsOfFile:savedImagePath] autorelease];
}
Hope it'll be helpful.

Adding images into the iPhone application

I have an application where the user should be able to take pictures using camera and save them within the application. By default all the images taken are saved to the photo album.But I want to bring them into my project just like the way we add required images for the project into the Resources folder in Xcode. At the same time I want them to converted into PNG format.
After you have taken image from Camera you can save this image to your application's document directory as,
UIImage *image = imageFromCamera;
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:#"MyImage.png"];
[imageData writeToFile:fullPathToFile atomically:YES];
Its tested code and working absolutely fine.
here is a great tutorial that you should check out http://iosdevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html
And here is the documentation from Apple for the UIImagePickerController that you should use https://developer.apple.com/documentation/uikit/uiimagepickercontroller
NSArray *UsrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocsDir = [UsrDocPath objectAtIndex:0];
NSString *path = [DocsDir stringByAppendingPathComponent:#"image.jpg"];
//Check if image exists on documents path, if exists removed it, but it depends on your logic, you can add more images in documents path by changing its name...
BOOL success = [FileManager fileExistsAtPath:zipPath];
if(success){
[FileManager removeItemAtPath:path error:&error];
}
[UIImageJPEGRepresentation(image,1.0) writeToFile:path 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.