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.
Related
I know its not a good question to be ask but i am stuck. How can i detect when user pick image from library not from camera and this library image saved via front camera or back camera? Like
if (library image from front camera)
{
// Do something here
}
else {
// Do something here
}
Your code checks for available cameras on the device. What you need to do is read the metadata for the image after you have taken the picture, that will include info on the camera.
Use this solution to read the Exif data that comes with the image to find out which camera obtained it: Exif Data from Image
You can check the image EXIF data in the info dictionary UIImagePicker passes in it's callback.
- (IBAction) handleTakePhoto:(UIButton *)sender {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
__block NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"%#", [metadata valueForKeyPath:#"{Exif}.LensModel"]);
[picker dismissViewControllerAnimated:YES completion:nil];
});
}
The above snippet outputs
iPhone 6 Plus back camera 4.15mm f/2.2
You would have to parse out the "front" or "back" parts of the string.
Relying on parsing something that is parsed out of a string raises some red flags -- there is probably a better and more stable way of doing it.
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];
}
Im getting a memory warning when Im using the camera on an iPhone. Im also using ARC.
When you take a photo and press the 'use Photo' button on the camera view controller I get a memory warning. The intention is once the 'use Photo' button is pressed that it changes the contents of the an ImageView.
I thought the memory issue might be due to the fact that the image that is captured is full screen, and the ImageView is 250h 250w. But I tried scaling down the size of the image taken by the camera and then assign it to the ImageView. However this still did not work, even when I resized it to 100 x 100.
Secondly, I then did not assign the photo taken by the camera to the ImageView but it still has the memory warning.
I looked at other answers here and attempted the two above but it is still there. I will show my code below. Will this affect my submission to the app store? Surely if it is such a common occurence that it is a bug or there is a work around? It would be great if one could look at the code provided and spot the error or suggest how to handle this memory warning?
My app is 95+% finished apart from this memory warning so it is getting close to submission time.
My code:
- (IBAction)takePhoto:(id)sender {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing=NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:self.imagePicker animated:YES completion:NULL];
}
else{
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker animated:YES completion:NULL];
}
}
- (IBAction)choosePhoto:(id)sender {
self.imagePicker2 = [[UIImagePickerController alloc] init];
self.imagePicker2.delegate = self;
self.imagePicker2.allowsEditing=NO;
[self.imagePicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker2 animated:YES completion:NULL];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
CGRect rect = CGRectMake(0,0,100,100);
UIGraphicsBeginImageContext( rect.size );
[self.image drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.snapImage setImage:picture1];
[self.uploadImageBtn setHidden:NO];
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
I didnt find a good solution but I would not store the raw image in a property because the raw image takes up roughly 30MB of memory. So instead of:
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
I changed it to:
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
This way the image is destroyed when it is no longer in use. Note: I've test this new method on iPhone 4 series and 5. The memory warning only appears on the 4 series not the 5.
From looking around the web there have been many bug reports submitted to Apple in regards to the Camera and iOS7. For instance, irregularly when you launch the Camera it will give a black preview - this is linked to iOS7, and more so the iPhone 4 series not 5. This is probably the difference in the processor power - but I am not sure. My app got approved for the app store so the memory warning will not be an issue –
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
Clearing the Cache in the class i was using the "UIImagePickerController", worked for me !!!
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.
I'm working on an app that gives the user the option to take a photo for their profile picture, but can't seem to figure out how to:
Get the photo to save to the users library
Get that photo to replace a default photo when they press "use" (that is there when the user first loads the app)
Any suggestions? This code might be completely off but here is what I was starting to use:
- (void)takePhoto {
UIImagePickerController *takePhotoPicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
takePhotoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
takePhotoPicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
} else {
takePhotoPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentViewController:takePhotoPicker animated:YES completion:nil];
}
What you need to do is register your viewController as a UIImagePickerControllerDelegate, and then do:
takePhotoPicker.delegate = self;
Then, you need to add the method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {}
which you can use to get the image.
To get the image from the camera or photo album you need to get the value from the correct key from the info dictionary.
For example, to get the Edited image (resized by user):
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
And to get the original image:
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
The original image is the full screen image that the user took with the camera.
You can then use this image to set an image view or upload to the server, etc.
Also, you shouldn't set the source type based on whether or not the camera is available, but you should let the user select (in case they want to choose from the photo album even if they have a camera).