简体   繁体   中英

UIButton doesn't change the image

i have an UIButton with Custom style and a default image. When i press it, i load the camera and when the photo is taken i want to change the image in the UIButton but it doesn't work.

I'm using this code:

- (void) imagePickerController: (UIImagePickerController *) picker
 didFinishPickingMediaWithInfo: (NSDictionary *) info {
    UIImage *originalImage, *imageToSave;
    originalImage = (UIImage *) [info objectForKey:
                                     UIImagePickerControllerOriginalImage];
    imageToSave = originalImage;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
    });
    [self.imageButton setImage:originalImage forState:UIControlStateNormal];
    hasPhoto = YES;
    [self dismissModalViewControllerAnimated: YES];

}

Any idea? Thanks!

Have you checked that self.imageButton is not nil?

You may have simply failed to bind your self.imageButton outlet to the button in your nib (it's easily done).

Or as Basically Bits suggests, the view (and any subviews including your button) may have been released when the image picker was shown, so you may need to force you view to reload before attempting to manipulate the button image. If that's the case, call this before setting your image to re-load the button:

[self view];

This may appear like it would do nothing, but the view property is a lazy constructor, so calling it will load the view from the nib again if it was unloaded.

您的代码看起来正确,是否检查了UIImagePickerController打开时是否释放了imageButton?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM