简体   繁体   中英

iOS Image rotates on button press

I am downloading some images from an API to my ios app and then displaying the images as a button background. Here's the code:

NSData *imageData = [r responseData]; 
UIImage *image =  [UIImage imageWithData:imageData];               
UIBlockButton *imageView = [UIBlockButton buttonWithType:UIButtonTypeCustom];       
[imageView setBackgroundImage:image forState:UIControlStateNormal];

Here's the weird part. Vertical images (Length is less than height) will rotate 90 degrees when I press the button. Has anyone experienced this before/is it a bug?

Edit: So my server sends the images in the rotated state. Somehow the UIButton knows to rotate it when it is not selected, but doesn't make the same correction when I select it.

Add [imageView setBackgroundImage:image forState:UIControlStateHighlighted];

Unless, of course, you prefer another image for the highlighted state to simulate the actual pressing of the button. In that case you will use the other Image in/instead of "image".

But you should get rid of that strange effect at least.

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