I have built a sample project on camera and library. Now, my question is can I store the images that I am selecting from library into an array? I am using this code.
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.arrImages addObject:chosenImage];
Now by placing breakpoints, I came to know that, in my array always 1object is storing even after I chosen images for more than one times.
Can anyone give my any idea how can I do it?
Any help is highly appreciated.
Try this.
-(void)viewDidLoad {
//declare before NSMutableArray *_mutableArray;
_mutableArray = [[NSMutableArray alloc] init];
}
....
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//dismiss UIImagePickerController
[picker dismissViewControllerAnimated:YES completion:Nil];
//take image from anywhere
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//store image into NSMutableArray
[_mutableArray addObject:image];
}
Related
I have setup the following to select an image whilst using an IPad. The problem is that the delegate never seems to get called. I've placed breakpoints in but they are never activated.
.H
#interface HomeViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
.M
- (IBAction)loadImage:(id)sender {
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePickerController.allowsEditing = NO;
self.imagePickerController.delegate = self;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
}
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//You can retrieve the actual UIImage
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Or you can get the image url from AssetsLibrary
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
[picker dismissViewControllerAnimated:YES completion:nil];
}
Can anyone see the issue?
Thanks
if you used Xcode8 to run the project, please check the project's info.plist, make sure there is a key for "Privacy Photo library usage".
your code is right, maybe the problem is the info.plist.
I had a similar issue and it turned out that the picker was being garbage collected as soon as the image was picked, so the delegate wasn't called.
I needed to ensure that a strong reference to the picker was made before presenting it.
Once that was done, it worked fine.
I am trying to save the contents of UIImageView into a variable that I can send to a server.
The contents of UIImageView is selected by the user from their camera role.
I cant seem to actually store the binary data / other format to send, how can I achieve this? Everything I have found on the net is saving the image into main memory which isnt what I want.
Edit:
Here is the actual image selection:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
If you want to send image to server. The only way to send it is by converting the UIImage to NSData .
NSData *imageData= UIImageJPEGRepresentation(image,0.0);
NSData *imageData = UIImagePNGRepresentation(image);
I am using static librrary and there is one class inside it that uses UIImagePickerViewController. I have set the delegate to it and written didFinishPickingMediaWithInfo: method within that class. What I want is to open front camera, capture the image when user clicks it and then save it to Photo Albums. What is happening with my code is that UIImagePickerController loads the camera and it saves the photo after capturing when I click on "Use Photo" button. But after that, it freezes but not crashes. I can't click anywhere after that. Any help appreciated. Thanks
// CODE IN LIBRARY
+ (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* photo = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data = UIImagePNGRepresentation(photo);
photo = [UIImage imageWithCGImage:[UIImage imageWithData:data].CGImage
scale:photo.scale
orientation:photo.imageOrientation];
UIImageWriteToSavedPhotosAlbum(photo, self, nil, nil);
// TRIED ALL OF THE BELOW BUT NOTHING WORKS FOR ME
[picker dismissViewControllerAnimated:NO completion:nil];
// [picker dismissModalViewControllerAnimated:YES];
[picker.view.superview removeFromSuperview];
// [picker release];
// picker=nil;
}
Your method shouldn't be static - remove the '+' and replace it with '-'.
Also, in the method you do
[picker.view.superview removeFromSuperview];
I'm not sure that's what you want to do !
So your code becomes :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
...
}
I'm really new to programming, I apologize in advance if this is a very simple question. I am trying to write a code to use UIImagePickerController to select two different images from my imageGallery and put it into two UIImageViews. Then, click a button to upload the images to Parse.
I'm having difficulty using UIImagePickerController twice and I can't seem to find an answer anywhere. Also, I'm having trouble linking it to the upload code. I've only learnt how to upload a file already in a folder, but I cant seem to work out how to link that to the UIImagePickerController. If you can help, it will be so much appreciated. My code so far...
- (IBAction)choosePhotoA:(id)sender {
UIImagePickerController *imagePickerControllerA = [[UIImagePickerController alloc] init];
imagePickerControllerA.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerControllerA.delegate =self;
imagePickerControllerA.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerControllerA animated:NO completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *imageA = [info valueForKey:UIImagePickerControllerOriginalImage];
self.imageViewA.image = imageA;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)uploadPhotoA:(id)sender {
PFObject *newImage = [PFObject objectWithClassName:#"photos"];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:#"newImage.png"]);
PFFile *newImageFile = [PFFile fileWithName:#"testPhoto.png" data:imageData];
[newImage setObject:newImageFile forKey:#"photoUploadOne"];
[newImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error){
NSLog(#"upload success!");
}
}];
}
When the image is picked and didFinishPickingMediaWithInfo: is called, you need to know which image was being picked (1 or 2). To do this, save a flag when the button is pressed which calls choosePhotoA:. Then, in didFinishPickingMediaWithInfo: you can use that flag to decide where to put the image (imageViewA or imageViewB). This would allow the user to change their mind and replace an image they already chose.
You can also save the images to disk if you want to, or use #propertys to hold references to them (you don't really want to go and get the images back from the image views).
Now, when you want to upload, get the images (from the properties, image views or disk) and upload (basically this just changes your current use of UIImage imageNamed:). And you need to run the code twice to process each image.
Hi is it possible to capture an Image with out saving to ios device .This is a question that is worrying me.
Can any please give me an idea how to achieve it.
Yes it is possible:
- (void)takePhoto
{
UIImagePickerController * pc = [[UIImagePickerController alloc] init];
pc.sourceType = UIImagePickerControllerSourceTypeCamera;
pc.delegate = self;
pc.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
pc.allowsEditing = YES;
[self presentViewController:pc animated:YES completion:^{
}];
}
#pragma mark - UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
UIImage * image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
self.imageView.image = image;
}];
}
Edit:
If you want to save the image you can simply save it to the Caches directory (see the apple docs for NSFileManager for info on how to do this, or other stack overflow questions. This is preferred to NSUserDefaults although that would work too.
If you want to simply send it (via email, share, or API upload) you dont have to save it first. You can use the in-memory version that resides in the self.imageView.image property above.