0

I am trying to browse an image and display in imageview using UIImagePickerController. Basically the view I am using to browse the image is based on navigationController.

But using the below code after I select the image and come back to view, I can see that the view is flickering to left right and the image is not showing in image view.

Actually the entire source code is quit long, thats why I post the image picker part only. If required I can post more code.

RegistrationFormViewController.h

@interface RegistrationFormViewController : ViewController
<UIImagePickerControllerDelegate,UINavigationControllerDelegate,CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    UIImagePickerController *imagePickerController;
}
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UIImageView *imageUser;

- (IBAction)registerBt:(id)sender;

RegistrationFormViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    _barButtonBack.target = self.revealViewController;
    _barButtonBack.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];


    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
    singleTap.numberOfTapsRequired = 1;
    [self.imageUser setUserInteractionEnabled:YES];
    [self.imageUser addGestureRecognizer:singleTap];
}



-(void)tapDetected{
    NSLog(@"single Tap on imageview");
    imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

#pragma mark - ImagePickerController Delegate

- (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 dismissViewControllerAnimated:YES completion:nil];
    [self.imageUser setImage:image];

     self.imageUser.contentMode = UIViewContentModeScaleAspectFill;
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
4
  • 1
    Why do you update self.imageUser.contentMode in the didFinishPickingImage callback? Commented May 19, 2016 at 6:05
  • Actually I am quiet new to ios, did that causing the problem, I have commented that section, but still the behavior is same. Commented May 19, 2016 at 6:10
  • 1
    At least update content mode first, then set the image, then dismiss the picker at last. Commented May 19, 2016 at 6:12
  • if you get the image or not? if you used AVAsset you will get original image but size to be reduced Commented May 19, 2016 at 6:13

3 Answers 3

1

There is no issue in your code that you showed here. However try this in didFinishPickingImage -

dispatch_async(dispatch_get_main_queue(), ^{
    self.imageUser.contentMode = UIViewContentModeScaleAspectFill;
     [self.imageUser setImage:image];

    });

and move this code to viewDidLoad

imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

you can also check whether you are getting the image or not using breakpoint.

Sign up to request clarification or add additional context in comments.

Comments

1

try this delegate method

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

     UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
     [picker dismissViewControllerAnimated:YES completion:NULL];
}

Comments

1

In viewcontroller.m file, just put this code:

#import <AssetsLibrary/AssetsLibrary.h>
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
- (IBAction)Gallery:(id)sender {

self.ImagePickerController = [[UIImagePickerController alloc]init];
self.ImagePickerController.delegate = self;
self.ImagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:self.ImagePickerController animated:YES completion:nil];
}

- (IBAction)Camera:(id)sender {

    self.ImagePickerController = [[UIImagePickerController alloc]init];
    self.ImagePickerController.delegate = self;
    self.ImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:self.ImagePickerController animated:YES completion:nil];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];


    NSURL *imageUrl  = (NSURL *)[info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *representation = [myasset defaultRepresentation];
        CGImageRef resolutionRef = [representation fullResolutionImage];

        if (resolutionRef) {
            UIImage *image = [UIImage imageWithCGImage:resolutionRef scale:1.0f orientation:(UIImageOrientation)representation.orientation];
            self.SelectedImage.image =image;

        }
    };
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"cant get image - %@",[myerror localizedDescription]);
    };

    if(imageUrl)
    {
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc]init];
        [assetslibrary assetForURL:imageUrl resultBlock:resultblock failureBlock:failureblock];
    }

}

In viewcontroller.h file

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic)UIImagePickerController *ImagePickerController;
@property (weak, nonatomic) IBOutlet UIImageView *SelectedImage;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.