UIImagePickerController didFinishPickingMediaWithInfo

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

    UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
    if(!img) img = [info objectForKey:UIImagePickerControllerOriginalImage];

    uiimvImagem.image = img; //why bother?
    uiimvImagem.alpha = 0; //why bother?
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:img];
    self.uiimvImagem = tempImageView; //Make sure your self.uiimvImagem is a retained property! Also, don't forget to nil it out in your dealloc method
    [tempImageView release];

    CGRect frame = uiimvImagem.frame;
    frame.size.width = 359;
    frame.size.height = img.size.height / (img.size.width / 359);
    uiimvImagem.frame = frame;

    uisv1.showsVerticalScrollIndicator = NO;
    uisv1.showsHorizontalScrollIndicator = NO;
    uisv1.contentSize = CGSizeMake(uiimvImagem.frame.size.width, uiimvImagem.frame.size.height);
    uisv1.maximumZoomScale = 3.0;
    uisv1.minimumZoomScale = 0.8;
    uisv1.clipsToBounds = YES;
    uisv1.delegate = self;

    [uisv1 addSubview:uiimvImagem];

    [self dismissModalViewControllerAnimated:YES];   

}

This should be a bit better - though I'm not sure about how the rest is hooked up (i.e. property declarations and I'm not sure where uisv1 is allocated.) There's still something that doesn't look right - you are assigning an image to an image view but then running the whole thing over with a newly allocated UIImageView.

Share:
13,765
Manoel Costa
Author by

Manoel Costa

Updated on June 04, 2022

Comments

  • Manoel Costa
    Manoel Costa almost 2 years

    Bellow is my didFinishPickingMediaWithInfo method and I know that I have a lot of unnecessary code, retains, releases, etc... But this was the way I could get it to work and I'm afraid of breaking it on trying to optimize it.

    Can anyone help me on taking off the unnecessary stuff and have this method clean?

    Also, I have a view controller that will load up 4 photos (Camera Roll or Camera) and after the 2nd or 3rd photo selection at the picker, the app crashes or at least, I got one of these alerts below. What I'm doing wrong? THanks!

    -2012-06-23 10:54:07.399 LookdoDia[6525:707] Received memory warning.

    -wait_fences: failed to receive reply: 10004003

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
        [self dismissModalViewControllerAnimated:YES];   
    
        UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
        if(!img) img = [info objectForKey:UIImagePickerControllerOriginalImage];
    
        uiimvImagem.image = [img retain];
        uiimvImagem.alpha = 0;
        UIImageView *tempImageView = [[UIImageView alloc] initWithImage:img];
        self.uiimvImagem = [tempImageView retain];
        [tempImageView release];
    
        CGRect frame = uiimvImagem.frame;
        frame.size.width = 359;
        frame.size.height = img.size.height / (img.size.width / 359);
        uiimvImagem.frame = frame;
    
        uisv1.showsVerticalScrollIndicator = NO;
        uisv1.showsHorizontalScrollIndicator = NO;
        uisv1.contentSize = CGSizeMake(uiimvImagem.frame.size.width, uiimvImagem.frame.size.height);
        uisv1.maximumZoomScale = 3.0;
        uisv1.minimumZoomScale = 0.8;
        uisv1.clipsToBounds = YES;
        uisv1.delegate = self;
    
        [uisv1 addSubview:uiimvImagem];
    
        [img release];
    
    }