Show part of UIImage in UIImageVIew

This question may have been answered, but I can’t find a solution to the problem after several hours of browsing the Internet.

I have UIImageView(320 * 320) in a custom UITableCell with UIImage(320 * 320) inside. When I click the button, the size UIImageViewchanges to 320 * 80 ( animated ) and UIImage too.

How can I do to prevent resizing UIImage? I would like this UIImageone to remain the same and show only part of this image (frame 320 * 80 with zero 0/0)

Edit: crop

I thought about cropping the image to the desired size, and then setting the UIImageView image to a full-size image, but it looks ugly, because when the user clicks on the UIImageView , it is still 320 * 320, but the UIImage is set to cropped (320 * 80) so it is stretched ...

thank

+3
source share
3 answers

I do something similar in one of my applications, when I create an object UIImageView, I set the property contentModeon UIViewContentModeScaleAspectFilland the property clipsToBoundson YES. Hope this helps.

+3
source

You can resize the image and then replace the UIImageView image with the resized image.

CGImageRef imageToBeResized = yourImage.CGImage;
CGImageRef partOfImageAsCG = CGImageCreateWithImageInRect(imageToBeResized, CGRectMake(0, 0, 320, 80));
CGRelease(imageToBeResized);
UIImage *resizedImage = [UIImage imageWithCGImage:imageToBeResized];
+3

, UIScrollView UIImageView . , .

+1
source

All Articles