Saving image in application documents - iphone

I try to save a image from camera or photo album into application directory. But i cant find what i'm doing wrong.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
theimageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"latest_photo.png"];
//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
if(fileExists){
NSLog(#"image Exists");
}
}
}

Why not use,
UIImage *orgImage = [info objectForKey:UIImagePickerControllerOriginalImage];
And where are failing? Did you get the image properly? or failing to write to document directory?

Check if the file in fact exist in file explorer. Maybe the compiler is right.

Related

How to get URL image in UIImagepickercontroller

I want to get URL image in UIImagepickercontroller after take picture.
I used following codes in didFinishPickingMedia..
NSData *webData = UIImagePNGRepresentation(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 *image1 = [UIImage imageWithContentsOfFile:localFilePath];
But in console print (null)
Can you show me ? Thanks
Image picker has two success delegate methods as follow:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;
// This is Deprecated in ios 3.0
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
It seemed to me that you are using the first method (You didn't mentioned the method but I thought to let you know). In this case you have to change your implementation to the second method.
For accessing UIImage and storing it to some path use as follow:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//Zoomed or scrolled image if picker.editing = YES;
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
// Original Image
UIImage *OriginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
// You can directly use this image but in case you want to store it some where
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDirPath stringByAppendingPathComponent:#"myImage.png"];
NSLog (#"File Path = %#", filePath);
// Get PNG data from following method
NSData *myData = UIImagePNGRepresentation(editedImage);
// It is better to get JPEG data because jpeg data will store the location and other related information of image.
[myData writeToFile:filePath atomically:YES];
// Now you can use filePath as path of your image. For retrieving the image back from the path
UIImage *imageFromFile = [UIImage imageWithContentsOfFile:filePath];
}
Hope this helps you :)

how to get the path of the image which is in iphone gallery

i need to upload a image from iphone gallery ,but i am not getting the path of the picked image (using image picker)
in the image picker delegate method i am doing the following
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSString *strImagePath = [info valueForKey:UIImagePickerControllerReferenceURL];
NSString *strImagePath1 = [info valueForKey:UIImagePickerControllerMediaURL];
NSLog(#"%#",strImagePath);
NSLog(#"%#",strImagePath1);
NSLog(#"%#",info);
}
and the info dictionary contains following key values
{
UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = "<UIImage: 0x16adc0>";
UIImagePickerControllerReferenceURL = "assets-library://asset/asset.PNG?id=1000000153&ext=PNG";
}
i used the value for key UIImagePickerControllerReferenceURL as the image path to upload it to FTP but i couldnt upload the file, probably the path which i taking is incorrect.
can anybody help me out how can i take the actual path of the picked image using image picker
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];
Convert your image in NSData and then Upload this NSData.
Your image in NSData
NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
Or you can convert yourImage in NSData using
NSData *dataImage = UIImagePNGRepresentation(yourImageView.image);
For more read NSData Class Reference

how to save capture the video in local file

I want capture the video using iPhone application. In this video file i want store the local application. how to save this file in locally. Please help me.
Thanks in Advance
You can do it :
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:#"/vid1.mp4"];
BOOL success = [videoData writeToFile:tempPath atomically:NO];
[picker dismissModalViewControllerAnimated:YES];
}

iPhone auto sync photos with our application using AlAssetLibrary

I have just started developing an application in which I want to auto sync all the images stored in user's photo library as assets.
I am able to get URL for images those are stored as assets.
e.g. assets-library://asset/asset.JPG?id=1000000003&ext=JPG
Now, how do I copy this image in to document's directory of my application or how do I just display image stored in assets by getting that in NSData or in UIImage?
Thanks in Advance.
You can use the following code in UIImagePickerControllerDelegate delegate implementation
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"my_photo.png"];
//extracting image from the picker and saving it
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *imageData = UIImagePNGRepresentation(editedImage);
[imageData writeToFile:imagePath atomically:YES];
}

How to Load an Image on the iPhone

I am using a UIImagePickerController, and once an image is selected, I save it to a PNG. But for some reason, I can't load the saved image.
Here is my code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//
// Create a PNG representation of the edited image
//
UIImage *anImage = [info valueForKey:UIImagePickerControllerEditedImage];
NSData *imageData = UIImagePNGRepresentation(anImage);
//
// Save the image to the documents directory
//
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Get a unique filename for the file
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
NSString *fileName = [(NSString *)string autorelease];
fileName = [NSString stringWithFormat:#"images/%#", fileName];
fileName = [fileName stringByAppendingString:#".png"];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:fileName] ];
[imageData writeToFile:path atomically:YES];
NSLog(#"Wrote the imageData to a file at path: %#", path);
// TEST TO SEE IF THE FILE WAS WRITTEN CORRECTLY
UIImage *testImage = [UIImage imageWithContentsOfFile:path];
NSLog(#"testImage is %#", testImage);
And here is my debugger output:
2010-12-28 16:29:08.676 Appster[6656:207] The imagePath is defined as: images/7ADB104E-DA45-4EE9-88DA-FF71B8D730CA.png
2010-12-28 16:29:08.712 Appster[6656:207] Wrote the imageData to a file at path: /Users/bschiff/Library/Application Support/iPhone Simulator/4.2/Applications/9F6FC2C7-9369-438C-AF8F-D5D25C72D8D7/Documents/images/FC94AA93-02EC-492A-A8FE-7D0C76E2C085.png
2010-12-28 16:29:08.715 Appster[6656:207] testImage is (null)
Have you tried to use NSBundle to refer to your resources path?
Try this one:
[[NSBundle mainBundle] pathForResource:[NSString stringWithString:fileName]
ofType:#"png" inDirectory:#"images"]]
Use that path to save and retrieve the image file