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];
}
Related
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];
}
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"];
}
i have view controller which contains a button which show the image library ,if the user tap the image,the select image will display in the same view with the button ,but i want to show this image in another class ?(view controller).my code is
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
it shows the image in the same view,but my need is to show it in another view.How to dod this.
Store the Image in a UIImage object and pass it to the viewController you want
In your Current View Controller
.h declare the a UIImage object
UIImage *selectedImage;
In .m file
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
selectedImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
imageView.image = selectedImage;
}
Pass it to the viewController you want
YourViewController *viewObj = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:[NSBundle mainBundle]];
viewObj.imageToBePassed = selectedImage;
//You should synthesize this object imageToBePassed
[self viewObj animated:YES];
[viewObj release];
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];
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];
}