Get image from gallery with UIPopover and show it - iphone

I'm trying to get image from gallery and than display it in UIImageView. My problem is in displaying this image. My code:
- (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.popController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
self.popController.delegate = self;
[self.popController presentPopoverFromRect:[fromGalaryButton frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self.popController dismissPopoverAnimated:YES];
self.image = [info objectForKey:UIImagePickerControllerEditedImage];
[self updateDisplay];
}
-(void)updateDisplay {
self.imageView.image = self.image;
self.imageView.hidden = NO;
[self.imageView reloadInputViews];
}
My self.imageView is displaying normally. But there is no any image in. There is my problem?

If you are just picking in image from the gallery then the line of code in didFinishPickingMediaWithInfo method should be:
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
You could also just update the imageView from within the method as such:
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
This is how I did this:
- (void)viewDidLoad {
[super viewDidLoad];
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:picker];
}
- (IBAction)getPhoto:(id)sender {
if(sender == choosePhotoButton) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[imagePickerPopover presentPopoverFromBarButtonItem:choosePhotoButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[imagePickerPopover dismissPopoverAnimated:YES];
imageViewer.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}

Related

can't show picking image from photo library

Im using image picker for choose photos from photo library. When i click the button it displays photo library. But when i click particular image it dismiss modalView and image won't load to view.
code:
-(void)showcamera:(id)sender{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePickerController animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
// Dismiss the image selection, hide the picker and
//show the image view with the picked image
[picker dismissModalViewControllerAnimated:YES];
UIImage *newImage = image;
}
Use this code:
in .h file
{
UIImageView *newImage;
UIImagePickerController *imagePicker;
}
in .m file
- (void)imageTaped:(UITapGestureRecognizer *)tapGesture {
imagePicker=[[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.allowsEditing =NO;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
//release picker
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//set image
newImage = [[UIImageView alloc] initWithImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
[self.view addSubview:mewImage];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
imgPicked = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
[imagePickerController dismissModalViewControllerAnimated:YES];
}
}
Declare this in .h
UIImage *imgPicked;
in ViewDidLoad
imgPicked = [[UIImage alloc]init];
Best Way is Add Your Image to UIImageView,
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:CGRectMake(#"As U Want")];
ImgView.image = image;
[self.View addSubview: ImgView];
[picker dismissModalViewControllerAnimated:YES];
}

How to use move and scale UIImagePickerController in iPhone?

I am new to iPhone development. I am working with image picker controller in which I want to move and scale the image that picked from the photo library, so I enabled the allowsediting = yes.
And my picker did finish method:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.imgForDisplay.image=[self scaleAndRotateImage:image];
self.imagePicker.allowsEditing = YES;
ImageDetailsViewController *imageDetailsVC = [[ImageDetailsViewController alloc] init];
imageDetailsVC.imagedisplay.image = self.imgForDisplay.image;
[picker pushViewController:imageDetailsVC animated:YES];
//[picker dismissModalViewControllerAnimated:YES];
}
and I push another view controller where I want to show the Image picked.
Here I have two problems:
1) When I select the image, its navigating to another view without "move and scaling" screen but the image is now displayed in the ImageDetailsViewController when it navigates.
2) When I again navigate back and pick another image, now it shows the "move and scaling" screen. When I click the choose button, it is simply showing a white screen with out navigation bar.
Is there any way to set the action for the choose button?
I too face the same problem i solved it by using the by using the code, go through this it will be usefull.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
self.image = shrunkenImage;
} else if ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
self.movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
}
ImageSelectionView *imageSelectionVC = [[ImageSelectionView alloc] init];
imageSelectionVC.theImage = image;
[picker pushViewController:imageSelectionVC animated:YES];
imageSelectionVC.dismissDelegate =self;
// [picker dismissModalViewControllerAnimated:YES];
}
'
-(void)updateDisplay {
if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
imageView.image = image;
imageView.hidden = NO;
moviePlayerController.view.hidden = YES;
} else if ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
[self.moviePlayerController.view removeFromSuperview];
self.moviePlayerController = [[MPMoviePlayerController alloc]
initWithContentURL:movieURL] ;
moviePlayerController.view.frame = imageFrame;
moviePlayerController.view.clipsToBounds = YES;
[self.view addSubview:moviePlayerController.view];
imageView.hidden = YES;
}
}
I think you would be better off presenting your picker modal like this:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
Then have your image displayed in the view of the viewcontoller that presented it.
-(IBAction)Btn_AddImage:(id)sender
{
UIImagePickerController *Image_Picker=[[UIImagePickerController alloc]init];
Image_Picker.delegate=self;
Image_Picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:Image_Picker animated:YES completion:Nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData *Data=UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"], 1);
UIImage *Cust_Image=[[UIImage alloc]initWithData:Data];
Profile_Image.image=Cust_Image;
[picker dismissViewControllerAnimated:YES completion:Nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:Nil];
}

A UIImagePickerController story: how to grab the image for insert in a tweet?

I have to add an image in a tweet. I tried this code, and only the picker shows. When I tap on the image it doesn't grab... Here is the code:
- (IBAction)sendImageTweet:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = NO;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[self dismissModalViewControllerAnimated:YES];
[tweetViewController addImage:(UIImage *)image];
[self presentModalViewController:tweetViewController animated:YES];
}
Thanks, and sorry for my English...
I think the problem is you are dismissing a viewController and presenting one at the same time. I did this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[self performSelector: #selector(showTweetwithPhoto:) withObject: image afterDelay: 0.5];
}
and it worked perfectly
-(void) showTweetwithPhoto:(UIImage*)image {
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
[tweetViewController addImage:image];
[self presentModalViewController:tweetViewController animated:YES];
}
Try casting the image data (returned by UIImagePickerController) to UIImage object, as shown below. (I have changed 2nd line of your code).
UIImage *image = (UIImage*)[info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImage * image = (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];

UIImagePickerController ios 5 issue when select the image uising UIPopoverController in ipad

i have the problem with the select the image from UIImagePickerController in ipad in ios 5.
same code was run perfectly in ios 4 .
so what is the problem ?
can any one help me how to use UIImagePickerController in ios 5 ?
Is there any difference between ios 4 and ios 5 for UIImagePickerController ?
Below is the code for the issue.
-(IBAction)selectExitingPicture:(id)sender
{
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
[popoverController release];
} else {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
popoverController = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake(280, 700, 320, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[imagePicker release];
}
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[popoverController dismissPopoverAnimated:true];
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
selectedImageView.image = image;
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}
}
After selecting the image from UIImagePicker in the imagePickerController:didFinishPickingMediaWithInfo method i received the image reference 0*0
Thanks
try this
-(IBAction)buttonpressed{
UIImagePickerController *anImagePickerController = [UIImagePickerController new];
anImagePickerController.delegate = self;
anImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"overlay_320x427.png"]];
anImageView.frame = CGRectMake(0, 1, 320, 427);
anImageView.hidden = YES;
anImagePickerController.cameraOverlayView = anImageView;
[self presentModalViewController:anImagePickerController animated:YES];
[anImagePickerController release];
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(timerFireMethod:)
userInfo:anImageView
repeats:NO];
[anImageView release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)info {
/* Do something here*/
[self dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}

multiple UIImageViews with UIImagePickers

I have 3 UIImageViews for which I want to load an image.
For 1 image this is not a problem, but how do I scale to 3?
The code below is what I have right now.
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
[my_image_1 setImage:image forState:UIControlStateNormal];
}
Have you looked at this or this? They seem to be pointing to this resource.
Edit
Based on your comment, I suggest you declare an ivar imageViewToSet in your class and alter your methods in the problem as below,
-(IBAction) getPhoto:(id) sender {
// Add code to identify the sender(button) via tags or outlets and set imageViewToSet to the mapped imageView through a switch or if statement.
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageViewToSet.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}