Edit UIImage in UIImageView

I'm trying to fit an image in uiimageview, images are uploaded and loaded dynamically, and only one resolution is available for ios and android applications.

So, I need images to maintain aspect ratio and scale to width, I set the UIImageView content mode to UIViewContentModeScaleAspectFill , but it centers the image, so it exits the screen both from above and below, the images will be designed so that the bottom is not needed.

How can I align the image in the upper left corner?

And can scaling UIImageView for me too? or how can i do this?

Thanks in advance.

EDIT:

I tried setcliptobounds and it reduces the size of the image to the image, this is not my problem.

UIViewContentModeTopLeft works well, but now I cannot apply UIViewContentModeScaleAspectFill , or can I apply both?

+6
source share
3 answers

You can scale the image according to the width of the image.

You can use the category on UIImage , which creates a new image with the selected width.

 @interface UIImage (Scale) -(UIImage *)scaleToWidth:(CGFloat)width; @end @implementation UIImage (Scale) -(UIImage *)scaleToWidth:(CGFloat)width { UIImage *scaledImage = self; if (self.size.width != width) { CGFloat height = floorf(self.size.height * (width / self.size.width)); CGSize size = CGSizeMake(width, height) // Create an image context UIGraphicsBeginImageContext(size); // Draw the scaled image [self drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)]; // Create a new image from context scaledImage = UIGraphicsGetImageFromCurrentImageContext(); // Pop the current context from the stack UIGraphicsEndImageContext(); } // Return the new scaled image return scaledImage; } @end 

That way you can use it to scale the image

 UIImage *scaledImage = [originalImage scaleToWidth:myImageView.frame.size.width]; myImageView.contentMode = UIViewContentModeTopLeft; myImageView.image = scaledImage; 
+9
source

UIView , which is a superclass of UIImageView , has a contentMode property. You can use the following constant to align the left edge of your image.

 UIViewContentModeTopLeft Aligns the content in the top-left corner of the view. 

To keep aspect ratio

 UIViewContentModeScaleAspectFit Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view's bounds is transparent. 
0
source

You cannot align and maintain aspect ratio with UIImageView, but there is a large subclass of UIImageView called UIImageViewAligned that makes this possible for you. You can find this project on github from here .
You need to do the following:

  • The first header and implementation of copying from UIImageViewAligned-master / UIImageViewAligned / to your project
  • Insert an ImageView object from the object library in IB into your view.
  • Change the ImageView class to UIImageViewAligned in the Identity Inspector from the Utilities panel
  • Then add the desired alignment as the key path in the "User-Defined Runtime Attributes" section of the Identity Inspector

What is it. Run the project to verify this.

0
source

Source: https://habr.com/ru/post/949975/


All Articles