UIimage not displaying image for duplicate method - ios

I have a UImage view that opens and you can take a picture with it and view it in the uiimageview. But I added another image view and copied the code and now the image shows up the same image as the second one. I believe it may have something to do with the '[UIImagePickerControllerOriginalImage];'
- (void)imagePickerController:(UIImagePickerController *)
picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
// Get the image and store it in the image view
image = info[UIImagePickerControllerOriginalImage];
self.personimgThumbNail.image = image;
}
- (void)imagePickerControllertwo:(UIImagePickerController *)
picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
// Get the image and store it in the image view
imagetwo = info[UIImagePickerControllerOriginalImage];
self.personimgThumbNailtwo.image = imagetwo;
}
Just need a next step, been stuck on this one for quite a while.

There can be only one didFinish-Method. You have to differentiate between what to do inside the method itself. The method already gives you the UIImagePickerController, which is calling the method, so you just have to compare the pointers to it.
- (void)imagePickerController:(UIImagePickerController *)
picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
if(picker == self.pickerController1){
// Get the image and store it in the image view
image = info[UIImagePickerControllerOriginalImage];
self.personimgThumbNail.image = image;
}else if(picker == self.pickerController2){
// Get the image and store it in the image view
imagetwo = info[UIImagePickerControllerOriginalImage];
self.personimgThumbNailtwo.image = imagetwo;
}
}
Edit: You have to have 2 properties defined in the .m file of your class
#property (strong) UIImagePickerController *pickerController1;
#property (strong) UIImagePickerController *pickerController2;
What you have to do now, when instantiating your image-picker is the following
(code taken from the OPs comment under this answer)
- (IBAction)accessPhotoLibrary:(id)sender {
if(!self.pickerController1){
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
self.pickerController1 = imagePicker;
}
[self presentViewController:self.pickerController1 animated:YES completion:nil];
}

Related

UIImagePickerController returns bigger Image than the originial

I am working on an app that let the users choose photo from the gallery. The problem I am facing is very weird as the size of the photo(in terms of storage) changes when its picked up using UIImagePickerController.
In my case, I got a picture via air-drop. The image size is 8.7MB. but when I pick the same image via UIImagePickerController, it returns me the image of ~13MB.
Note: the resolution of the image remains the same([3024, 4032]).
I created a very simple app to test the thing. Here is sample code:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
bool flag = true;
- (void)viewDidAppear:(BOOL)animated {
if (flag) {
flag = false;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[self presentViewController:picker
animated:YES
completion:NULL];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
NSData *temp = UIImageJPEGRepresentation(chosenImage, 1);
NSLog(#"image: %lu", (unsigned long)temp.length);
NSLog(#"image: [%lu, %lu]", (unsigned long)chosenImage.size.width, (unsigned long)chosenImage.size.height);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES
completion:NULL];
}
#end
this is the link to the app, in case you want to test it for yourself.
The zip file also contains the sample photos.
Any help is appreciated.
This is because you get the image uncompressed from the picker (as a UIImage). You then convert it to jpeg with UIImageJPEGRepresentation() and you are passing in 1.0 as the compression quality which is highest quality (least compression). If you pass a lower number like 0.5 for the quality the resulting data will be smaller. See: https://developer.apple.com/reference/uikit/1624115-uiimagejpegrepresentation?language=objc
update
If you want the original file data you can use UIImagePickerControllerReferenceURL, see here: How do I get the data from UIImagePickerControllerReferenceURL?
Please refer this code
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage* chosenImage = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
NSLog(#"Image Size Width %f Height %f",chosenImage.size.width,chosenImage.size.height);
UIGraphicsBeginImageContext(CGSizeMake(320, 480));
[originalImage drawInRect:CGRectMake(0,0,320,480)];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(#"image Size h %f Width %f",[image size].height, [image size].width);
}

Xcode set button image from camera

I have 2 questions relating to button images:
Setting a button image
Changing an image to a circular image
I have an iPhone application that has a UIButton created in Storyboard. Using the Attributes Inspector, I have set the buttons image to be an image that I created and added to the project.
The button will allow users to add a profile picture.
When I click the button, I ask the user to either use the phones camera to take an image or to select an image from camera roll.
This all works and I can access the camera to take a picture as well as camera roll image gallery to select an image.
My question is:
1.
How do I set the button image to the user-selected image?
In my didFinishPickingMediaWithInfo method I have:
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Set the image of the Button to the selected image
[sender setImage:image forState:UIControlStateNormal];
But I get error "Use of undeclared identifier" - because my button isn't the sender.
How do I set my button image as the image from the camera or camera roll image? If I create the button programatically I can set the image but it seems like a lot of unnecessary work to do if the button is created in Storyboard already?
Or do I need to create the button programatically to do this?
2.
I try to mask the image to have a rounded appearance in my didFinishPickingMediaWithInfo but I am unable to do so. How do I do this? I can make the image rounded by pacing the code below in my viewDidLoad but it does not work in my didFinishPickingMediaWithInfo
self.image.layer.cornerRadius = self.profileImageView.frame.size.width / 2;
self.image.clipsToBounds = YES;
Here is an example:
#import "MyViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface MyViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIButton *profilePictureButton;
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// make the button a circle.
_profilePictureButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
_profilePictureButton.imageView.layer.cornerRadius = _profilePictureButton.frame.size.width / 2.f;
}
- (IBAction)takePicture:(id)sender
{
UIImagePickerController *imagePickerController = [UIImagePickerController new];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController
animated:YES
completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[_profilePictureButton setImage:image forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:YES
completion:nil];
}
#end
You have to make sure that the button's type is UIButtonTypeCustom or this won't work at all.
Make sure you have an IBOutlet set up for the UIButton as cameraButton, and make sure your UIButton is set to a "Custom" type.
Make your view controller conform to the
#interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Tie your UIButton's TouchUpInside event to the following IBAction:
-(IBAction)showCameraAction:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
In your view controller, put the following delegate implementation methods.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.cameraButton setImage:chosenImage forState:UIControlStateNormal];
self.cameraButton.clipsToBounds = YES;
self.cameraButton.layer.cornerRadius = (self.cameraButton.frame.size.width / 2);//half of the width
self.cameraButton.layer.borderColor=[UIColor blackColor].CGColor;
self.cameraButton.layer.borderWidth=1.0f;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I did all this, and ended up with a button I could tap that would bring up the camera, and after taking a picture, would display a circular mask of the image with a black border around it. Feel free to remove the border by setting borderWidth to 0.

Photo album and editing?

We have to let the user open the photo album, pick an image, and edit it .
So using the picker, i can see all the user albums, than when enter an album i can see the images, than, when tapping an image, i get the delegate .
Why i don't see the check sign when choosing an image ? its unclear that you actually pick it. can you also pick more than one, or is it still disabled ?
I can't edit the selected image, after i pick it, i got the delegate called, but i don't have the editing button anywhere .
UIImagePickerController *pickerLibrary = [[UIImagePickerController alloc] init];
pickerLibrary.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerLibrary.delegate = self;
pickerLibrary.editing=YES;
[self presentViewController:pickerLibrary animated:YES completion:nil];
-(void)imagePickerController:
(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
UIImage *imageE = info[UIImagePickerControllerEditedImage];
NSLog(#"%#",image);
NSLog(#"%#",imageE);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Media is a video
}
// Code here to work with media
[self dismissViewControllerAnimated:YES completion:nil];
}
Ok , answer is this :
pickerLibrary.allowsEditing=YES;
and not isEditing

how I can assign for each UIImageView in the same view a picture taken from camera?

I have two UIImageView in the same view.for each imageView I should assign a picture taken from the camera or photos library.
When I start by doing that, I get the same photo for the two imageView.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
imageView2.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
-(IBAction)addPhoto:(id)sender{
UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:#"" delegate:self cancelButtonTitle:#"cancel" destructiveButtonTitle:#"Choose Photo" otherButtonTitles:#"Take Photo ", nil];
[actionSheet showInView:self.view];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
shouldUpdateFirstImage =YES;
// chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
if (shouldUpdateFirstImage) {
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = NO;
}
else {
pictureView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = YES;
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Put Picture"])
{
alrtView.hidden = YES;
pictureView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:pictureView];
[self addPhoto:self];
}
And the viewController.h
#interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate,UITextViewDelegate>
{
BOOL shouldUpdateFirstImage;
}
The first ImageView is the background.Through an AlertView I choose to add Picture to the first ImageView.After that I recall the same actionsheet to choose the taken photo.
TL;DR you need to track the state of your class, and have your delegate method respond accordingly to that state.
It sounds like you need your class to keep track of how many images have been displayed, and load new selections based on that information. We call this "state", and you have to implement the logic for your imagePickerController delegate method to respond appropriately to that state.
One possible approach would be to have a BOOL scoped to your class (either an ivar or property will do, but ivar is likely more appropriate), then use it's value to keep track of which imageView should be updated.
Assuming you already declared a BOOL named shouldUpdateFirstImage, you could do something like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
if (shouldUpdateFirstImage) {
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = NO;
}
else {
imageView2.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = YES;
}
}
This would alternate which imageView was updated with each successive selection.
Try this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
if(imageView.image)
imageView2.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
else
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}

Sorting a Picture to a Specific Place in the app

I want it to flow like this I'm doing it tons of times and dosent work.
User pushes the photo button in the app and takes picture.
After taking the picture users has to input detail. sorts options comes out where user can pick 8 different default genre's, and user pushes save.
It goes back to the Home Screen and the User can see the 8 different genre in button & when pushed pictures comes out as a coverflow(flow cover) that is saved in the app. I want to make it work like the above but dosent work.
My Code is until now is:
#implementation ViewController
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType: UIImagePickerControllerSourceTypeCamera ];
[self presentViewController:picker animated:YES completion:NULL];
}
-(IBAction)ChooseExisting{
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
[picker2 setSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker2 animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *) info {
image = [info objectForKey:UIImagePickerControllerOriginalImage] ;
[imageview setImage:image];
[self dismissViewControllerAnimated:YES completion:NO];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
#end
#implementation CVCLCoverFlowLayout
-(NSInteger)count {
return [self.collectionView numberOfItemsInSection:0 ];
}
-(CGSize)collectionViewContentSize{
CGSize size = self.collectionView.bounds.size;
size.width = self.count * self.cellInterval;
return size;
}
You are not saving the image you clicked in the delegate function .and when you want to use the saved image then you need to write down the code for same .:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *) info {
image = [info objectForKey:UIImagePickerControllerOriginalImage] ;
[imageview setImage:image];
/ save the image to local storage of your application /
[self dismissViewControllerAnimated:YES completion:NO];
}
You can check the code for same over :
http://dcraziee.wordpress.com/2013/05/20/how-to-save-and-get-image-from-cache-directory/
Please share the blog if you like it.

Resources