iPhone auto sync photos with our application using AlAssetLibrary - iphone

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

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

Saving image in application documents

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.

How do I save a reference to a specific photo on iPhone for loading without picker?

A user can select a photo from their library using the image picker - I want to remember this selection and display this picture in future without the user having to pick it. How can I reference the specific photo or is there a way I can copy the photo to my application's storage?
you can save the selected image by converting it into NSData object and then writing it on the iphone file system under your app's document directory like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [documentsDirectory stringByAppendingPathComponent:#"SavedImage"];
[imageData writeToFile:filename atomically:NO];
}