How to make UIImageView automatically resize to the size of the image loaded
I have put an UIImageView control on my view with IB.
The size of the control is just something I decided upon, pretty random size really
What I want to do is the control to resize automatically whenever I set the image property to a new image. I want it to actually resize to the size of the image.
Can it be done automatically ? without any code intervention ?
If not – what will the best approach be in this case ?
What happens today is strange. I load images into the ImageView and I see the images getting displayed properly even though the size of the ImageView is not changed. This interferes with my intention of grabbing users touches over the ImageView. The user touches the actual image, but since some parts of the image are outside ( and this is the strange part ) of the ImageView – point mapping goes crazy
Can someone think of any explanation to this ?
- NSDecimalNumber decimalNumberWithString: ignores current locale
- Unable to simultaneously satisfy constraints Warnings with AVPlayerViewController embedded in storyboard
- ios 8: Bundle path changes
- Resetting iPhone to factory settings programmatically
- How to capture image without displaying preview in iOS
- How to config my server to providing the video file play with MPMoviePlayerViewController?
Solutions Collect From Internet About “How to make UIImageView automatically resize to the size of the image loaded”
The size of an image has no bearing on how large the UIImageView actually is, rather the size of the UIImageView solely depends on the size given to it in Interface Builder (or that you assigned to it). Else the images would be all whacky when you use the @2x images for Retina displays for example.
If you want to fix this, you must change the frame when setting the image as well. If you’re doing this now:
[imageView setImage:[UIImage imageNamed:@"myImage.jpg"]];
change it to:
UIImage img = [UIImage imageNamed:@"myImage.jpg"]; [imageView setImage:img]; imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, img.size.width, img.size.height);
This will however not change the layout of view it is contained within, you can make it change the sizes of the other views automatically under iOS 6 using Layout Constraints. If you are an Apple Developer you can watch the WWDC instruction videos, they explain how that system works quite well.
If you’re fine with the view not growing, and the problem is just how the image overflows it’s bounds when you change it to one that does not match the dimension of the containing view, you can set the “Clip Subviews” checkbox in Interface Builder for the image view. This will make it so that the view will not draw anything outside it’s own bounding box, if you also set the scaling mode to “Aspect Fill” or “Scale To Fill”, the image will always fill up the entire bounds of the containing view.
- How to create a button programmatically?
- Overwriting the previous file in NSDocumentDirectory
- UITapGestureRecognizer breaks UITableView didSelectRowAtIndexPath
- Import RealmSwift no longer working using CocoaPods
- No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=armv7, VALID_ARCHS=armv6 i386)
- NSAttributedString background color and rounded corners
- Interpret XMP-Metadata in ALAssetRepresentation
- How to get a blinking button? (two alternating pictures)
- Get first element of array of pairs (Swift)
- What's the proper way to use an Objective-C category within Swift?
- UITableViewCell without using deprecated method initWithFrame:reuseIdentifier
- Programmatically navigate to another view controller/scene
- iOS8 scale glitch when calling drawViewHierarchyInRect afterScreenUpdates:YES
- CI Filter to create Black & White image?
- Lazy var giving 'Instance member can not be used on type' error