Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to resize an image for a custom UIButton?
    primarykey
    data
    text
    <p>I have a UIButton that needs to change its image once the user has selected a photo from the photo album to show its selection, I have this setup up and running but once the user has selected a photo I need to resize it to the button dimensions and that is where I need help, here is my code:</p> <pre><code>-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:YES]; UIImage *giftImage = [info objectForKey:UIImagePickerControllerOriginalImage]; [giftImage resizableImageWithCapInsets:UIEdgeInsetsMake( 0, 10, 0, 10 )]; [giftButton setBackgroundImage:giftImage forState:UIControlStateNormal]; giftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; giftButton.imageView.contentMode = UIViewContentModeScaleToFill; giftButton.imageView.bounds = CGRectMake( 0, 0, 62, 62 ); } </code></pre> <p>UPDATE: Found a solution, so far all that happen was that the button got resized to the image dimensions, but using the following code, I manage to insert the image inside at the size I need, it is a work in progress but is working ;)</p> <pre><code>CGSize targetSize = CGSizeMake( 62, 62 ); UIGraphicsBeginImageContext( targetSize ); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = CGPointMake( 0, 0 ); thumbnailRect.size.width = 62; thumbnailRect.size.height = 62; [giftImage drawInRect:thumbnailRect]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); [giftButton setBackgroundImage:newImage forState:UIControlStateNormal]; UIGraphicsEndImageContext(); </code></pre> <p>The code is from this post <a href="https://stackoverflow.com/questions/1367384/resize-and-crop-images-before-displaying-in-uitableviewcells">Resize and crop images before displaying in UITableViewCells</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload