I've a question in mind that driving me crazy. I've searched a lot to solve it, but all the answers that I found were old and not helped.
I'm currently working on application with that uses UIImagePickerControllerDelegate and with didFinishPickingMediaWithInfo I want to save the selected image to application folder.
Until now I'm able to select the image with this;
- (IBAction)btnPressed:(UIButton *)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
imgPicker.allowsEditing = NO;
[self presentViewController:imgPicker animated:YES completion:nil];
}
}
but couldn't save it to application using didFinishPickingMediaWithInfo;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *) kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//Assigning the selected image to imageView to see on UI side.
imageViewerImage.image = image;
}
}
I wonder what should be the part after this point.
I appreciate any help that you can provide.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
UIImage* image;
if([[info valueForKey:#"UIImagePickerControllerMediaType"] isEqualToString:#"public.image"])
{
image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:#"New Folder"];
// New Folder is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *fileName = [stringPath stringByAppendingFormat:#"/image.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
[data writeToFile:fileName atomically:YES];
}
}
Try this code. It may helps you. :)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
imgViewProfile.image = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:^{ }];
// ------ Now save this image to document directory by getting its path
}
Save an UIImage to the to Documentsfolder like this:
UIImage* myUIImage = ...
NSData *pngData = UIImagePNGRepresentation(myUIImage);
//NSData *pngData = UIImageJPEGRepresentation(myUIImage,0.5); //alternative comressed jpg instead of png
NSString *filePath = [[self buildDocumentsPath] stringByAppendingPathComponent:#"pictureName.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
Get the path of the documents as a string:
- (NSString *)buildDocumentsPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
return documentsPath;
}
Just save the image in your app document directory :
// first find the path in which to save the image :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *baseDir = [paths objectAtIndex:0];
// then save image as JPEG
NSString* filename = [baseDir stringByAppendingPathComponent:#"filename.jpg"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// or PNG
NSString* filename = [baseDir stringByAppendingPathComponent:#"filename.png"];
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
//In Your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage (UIImage *)image editingInfo:(NSDictionary *)editingInfo . Write this code
NSData *pngData = UIImagePNGRepresentation(image);
//This pulls out PNG data of the image you've captured. From here, you can 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
I did that in my project:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage * imageDone = [info objectForKey:#"UIImagePickerControllerEditedImage"];
[self saveImage:imageDone];
[self.popover dismissPopoverAnimated:YES];
}
-(void)saveImage:(UIImage*)image{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * imgpath = [documentsDirectory stringByAppendingFormat:#"/media/img/"];
long x = arc4random() % 10000;
NSString * namePic = [NSString stringWithFormat:#"%lld%ld-photo.jpg", self.idToLoad.longLongValue, x];
NSString * stringName = [NSString stringWithFormat:#"%#%#", imgpath, namePic];
[UIImageJPEGRepresentation(image, 0.3) writeToFile:stringName atomically:YES];
}
Related
I'm not quite sure whats going wrong here, I'd really appreciate some help as I'm very stuck.
This is my imagePickerController
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithLong:customphoto1] forKey:#"customphoto1count"];
UIImage *button1Image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
pngData = UIImagePNGRepresentation(button1Image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"Button1image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
[self dismissViewControllerAnimated:YES completion:NULL];
pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *imageLoaded = [UIImage imageWithData:pngData];
[_button1 setImage:imageLoaded forState:UIControlStateNormal];
self.button1.clipsToBounds = YES;
self.button1.layer.cornerRadius = (self.button1.frame.size.width / 16);//half of the width
self.button1.layer.borderColor=[UIColor blackColor].CGColor;
self.button1.layer.borderWidth=1.0f;
[self dismissViewControllerAnimated:YES completion:NULL];
}
The above saves the image (as far as I believe) since _button1 is reading from imageLoaded
When the app closes and is reopened, this is the what calls the button to be displayed, and should load the picture from my NSData:
-(void)customButton1{
if (customphoto1 == 1) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"Button1image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES];
pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *imageLoaded1 = [UIImage imageWithData:pngData];
NSLog(#"%#", filePath);
// UIImage *imageload1 = [UIImage imageWithData:[newManagedObject valueForKey:#"imagesone"]];
[_button1 setImage:imageLoaded1 forState:UIControlStateNormal];
}
else {
button1ImageName = #"addnew";
}
}
Why is my image not loading?
Many thanks
Try this:
-(void)customButton1{
if (customphoto1 == 1) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"Button1image.png"];
UIImage *imageLoaded1 = [UIImage imagewithcontentsoffile:filePath];
NSLog(#"%#", filePath);
[_button1 setImage:imageLoaded1 forState:UIControlStateNormal];
}
else {
button1ImageName = #"addnew";
}
}
Debug: Check whether the filepath which is getting printed is correct or not. Open this path in Finder > CMD + Shift + G and paste the path (remove the file name from the path, use till folder level). Then check if that particular image is available in that folder or not.
Also in your didFinishPickingMediaWithInfo method, you are calling dismissViewControllerAnimated method two times, it should be once and at the end of method.
Hope this helps
Image was saving and loading alright all along - issue turned out to be with the buttons setImage function
i would like to save 2 images as they get picked to separate names.
i used this guide to make my uiimagepickercontroller : guide
basically it's one uiimagepickercontroller that manages 2 uiimageviews, is it possible to make it save 2 different images with separate names ? or should i try to make uiimagepickercontroller different ?
here's my code, and it not's working for saving.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
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];
}
switch (_selectedPhotoType) {
case imageView1Type:
imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
case imageView2Type:
imageView2.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{ }];
}
Take a look at this implementation. Is that you need?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
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];
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
switch (_selectedPhotoType) {
case imageView1Type: {
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"latest_photo1.png"];
imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
break;
case imageView2Type: {
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"latest_photo2.png"];
imageView2.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
break;
default:
break;
}
[webData writeToFile:imagePath atomically:YES];
[self dismissViewControllerAnimated:YES completion:^{ }];
}
figured it out with little help from #Heximal
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
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];
switch (_selectedPhotoType) {
case imageView1Type: {
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"latest_photo1.png"];
imageView1.image = [info objectForKey:UIImagePickerControllerEditedImage];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
}
}
break;
case imageView2Type: {
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"latest_photo2.png"];
imageView2.image = [info objectForKey:UIImagePickerControllerEditedImage];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
}
}
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{ }];
}
I am using below code to save and read saved clicked image file from document directory. Its working well for the first time but when i'm trying to read the saved image file for the second time, its showing me the image clicked during first time.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"test.png" ];
NSFileManager* fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
NSError* error;
[fileManager removeItemAtPath:path error:&error];
}
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
}
Any idea why every time its returning me image clicked for the first time.
didFinishPickingImage: is deprecated since iOS3.
This should work:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (!image) {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if (image) {
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];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Please check the documentation.
I have the following code which launches the camera app and the user can select "use photo". However nothing happens.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePickerController animated:YES];
}
How I can I get an UIimage from this and save it to the photo album. Ideally I'd like to save this to the documents directory and know how to do that part. Thanks!
-(void) imagePickerController:(UIImagePickerController *)UIPicker didFinishPickingMediaWithInfo:(NSDictionary *) info
{
UIImage* originalImage = nil;
originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerCropRect];
}
[addImageOutlet setImage:originalImage forState:UIControlStateNormal];
[self saveBackgroundImageInDocumentDirectory:originalImage];
[self dismissViewControllerAnimated:YES completion:nil];
}
Use this code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData * imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
[imageData writeToFile:savedImagePath atomically:NO];
[picker dismissViewControllerAnimated:YES completion:nil];
}
I think it will help you,
//you have to set the delegate which clicking the button of camera
imagePickerController.delegate = self;
//This Delegate method will call
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage* selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSString *path = [[self pathToPatientPhotoFolder] stringByAppendingPathComponent:[NSString stringWithFormat:#"imageName.png"]];
NSError * error11 = nil;
[post.getData writeToFile:path options:NSDataWritingAtomic error:&error11];
}
//This method is useful for the get the Documentry path
- (NSString *)pathToPatientPhotoFolder {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *myalbumpath = [documentsDirectory stringByAppendingPathComponent:#"MYAlbum"];
// Create the folder if necessary
BOOL isDir = NO;
NSFileManager *fileManager = [[NSFileManager alloc] init];
if (![fileManager fileExistsAtPath:myalbumpath
isDirectory:&isDir] && isDir == NO) {
[fileManager createDirectoryAtPath:myalbumpath
withIntermediateDirectories:NO
attributes:nil
error:nil];
}
return myalbumpath;
}
Enjoy the coding
addImageOutlet for what type of object
I have written code to capture image from camera and i want to save that image in document directory. I have done the code but i think the image is not storing document directory. Because when i retrieve the images the camera images is not in documents directory,my code:
- (IBAction)takePhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *cameraImage = info[UIImagePickerControllerEditedImage];
self.tempView.image = cameraImage;
[_chosenImages addObject:_tempView.image];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
id n = [array3 lastObject];
NSLog(#"The id is=%#",n);
NSError *error;
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *dataPath = [aDocumentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",n]];
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSInteger anIndex = 0;
for (UIImage *anImage in _chosenImages) {
NSString *anImageName = [NSString stringWithFormat:#"%d.png", anIndex++];
NSString *anImagePath = [NSString stringWithFormat:#"%#/%#", dataPath, anImageName];
NSLog(#"the path is=%#",anImagePath);
NSData *anImageData = UIImagePNGRepresentation(anImage);
[anImageData writeToFile:anImagePath atomically:YES];
// While fetching NSDocumentDirectory Path it will give in the array format. So instead of giving NSString you need to give NSArray.. check below code
// Try this
NSArray * aDocumentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [aDocumentsDirectory objectAtIndex:0];
NSString *finaldocPath = [docPath stringByAppendingPathComponent:fileName];