pankaj
pankaj

Reputation: 8348

not able to save UIImage from iphone photolibrary

I am trying to save a image by picking it from iphone photo library. But I am not able to save it. My UIImage object suddenly releases. I am getting following error: * -[UIImage release]: message sent to deallocated instance 0x83fa5b0

Please check my code below:

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

    NSLog(@"finish picking media");

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]){

        UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSLog(@"found an image");

        selectedImage=[self scaleAndRotateImage:selectedImage];
        NSLog(@"image scaled");
        //add image to mainpic
        imgvwProfile.image = selectedImage;
        NSLog(@"11");
        //[ setImage:selectedImage forState:UIControlStateNormal];
        self.imgPic=selectedImage;
        //self.isMainPicImageModified=YES;
        NSLog(@"22");
        //[self sendImage:selectedImage];
        NSLog(@"selectedimage width:%f ht:%f",selectedImage.size.width,selectedImage.size.height);
        [self saveImage:selectedImage withImageName:@"profilePic.png"];
        NSLog(@"33");
    }
}

The error appears when NSLog(@"11"); has been execute. Can some one please help me why I am getting this error?

Thanks Pankaj

Upvotes: 1

Views: 164

Answers (2)

Kay
Kay

Reputation: 13146

If the error occurs immediatly after NSLog(@"11") has been executed (you see 11 in the console right?), then it's happening while releasing the former UIImage of self.imgPic. To verify this just put self.imgPic = null; at the beginning of the method.

If so check your code for former release calls on imgPic or other variables pointing to the same UIImage instance. Maybe in scaleAndRotateImage or other methods.

Upvotes: 0

Deeps
Deeps

Reputation: 4577

Can you change below line and check whether this statement makes problem or not.

 imgvwProfile.image = selectedImage;
                To
 imgvwProfile.image = [selectedImage retain];

Let me know now your app crash in this function or not.

Upvotes: 1

Related Questions