Need help with contentMode and autoresizingMask UIImageView

I have an image similar to this one that will be presented in landscape mode

alt text

therefore, if you rotate the device in Portrait, how to do it,

alt text

+7
iphone uiimageview
source share
4 answers

Use UIImageView with these settings:

imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); imageView.contentMode = UIViewContentModeScaleAspectFit; 
+12
source share

It looks like what you are trying to do is pin the image to the right, leaving the top, left and bottom the same.

Two attempts:

1) Subclass UIView and in the drawRect method adjust the rectangle and pull out the cropped image using CGContextDrawImage (context, rect, sourceImage.CGImage) .

2) Use CALayers. You can customize the "contentRect" of the imageView layer:

 CALayer* imageLayer = imageView.layer; imageLayer.masksToBounds = YES; CGRect rect; if (inPortrait) rect = CGRectMake(0.0, 0.0, 0.5, 1.0); // half size else rect = CGRectMake(0.0, 0.0, 1.0, 1.0); // full size imageLayer.contentsRect = rect; 

This reduces the image in half in width. masksToBounds takes care of clipping sublayers (if any). You can customize the contentsRect device rectangle to customize where you want to crop. You can also adjust your own imageView frames to fit your size.

This added bonus is that the contentRect setting is automatically animated, so when you do the rotation, the width of the animation is in and in good condition.

+7
source share
 // Set the layer contents view.layer.contents = [image CGImage]; // Stretch so that contents overflows the edges and keeps aspect ratio view.layer.contentsGravity = kCAGravityResizeAspectFill; // Clip off the overflow view.layer.masksToBounds = YES; // Set the frame CGRect frame; frame.origin = CGPointZero; frame.size = view.superview.bounds.size; view.frame = frame; // Make sure it resizes when the superview does view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
+2
source share

You will have to work with the view layer.

You can find relevant recommendations at https://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html

Hope this helps.

Thanks,
Jim.

0
source share

All Articles