UIImageView corresponds to the width

I have a UIImageView that is inside a UIScrollView . The image in the image view is larger than the iPhone screen. I want to make the image proportionally scaled so that it matches the width of the screen without changing the aspect ratio of the image (part of the image will be off the screen at the bottom).

I tried using UIViewContentModeScaleAspectFill and UIViewContentModeScaleAspectFit , but not doing what I want.

Any ideas?

Thanks.

+4
source share
2 answers

Here's the image class that I wrote to perform some simple functions, such as fitInsideWidth - this is the function that should be applied here:

MyImage.h

 #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface MyImage : NSObject + (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width covertToHeight:(float)height; + (UIImage*)imageWithImage:(UIImage *)image convertToHeight:(float)height; + (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width; + (UIImage*)imageWithImage:(UIImage *)image fitInsideWidth:(float)width fitInsideHeight:(float)height; + (UIImage*)imageWithImage:(UIImage *)image fitOutsideWidth:(float)width fitOutsideHeight:(float)height; + (UIImage*)imageWithImage:(UIImage *)image cropToWidth:(float)width cropToHeight:(float)height; @end 

MyImage.m

 #import "MyImage.h" @implementation MyImage + (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width covertToHeight:(float)height { CGSize size = CGSizeMake(width, height); UIGraphicsBeginImageContext(size); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newimage; } + (UIImage*)imageWithImage:(UIImage *)image convertToHeight:(float)height { float ratio = image.size.height / height; float width = image.size.width / ratio; CGSize size = CGSizeMake(width, height); UIGraphicsBeginImageContext(size); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newimage; } + (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width { float ratio = image.size.width / width; float height = image.size.height / ratio; CGSize size = CGSizeMake(width, height); UIGraphicsBeginImageContext(size); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newimage; } + (UIImage*)imageWithImage:(UIImage *)image fitInsideWidth:(float)width fitInsideHeight:(float)height { if (image.size.height >= image.size.width) { return [MyImage imageWithImage:image convertToWidth:width]; } else { return [MyImage imageWithImage:image convertToHeight:height]; } } + (UIImage*)imageWithImage:(UIImage *)image fitOutsideWidth:(float)width fitOutsideHeight:(float)height { if (image.size.height >= image.size.width) { return [MyImage imageWithImage:image convertToHeight:height]; } else { return [MyImage imageWithImage:image convertToWidth:width]; } } + (UIImage*)imageWithImage:(UIImage *)image cropToWidth:(float)width cropToHeight:(float)height { CGSize size = [image size]; CGRect rect = CGRectMake(((size.width-width) / 2.0f), ((size.height-height) / 2.0f), width, height); CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect); UIImage * img = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return img; } @end 
+10
source

You must program the zoomScale scroll:

 yourImageView = [[UIImageView alloc] initWithImage:yourImage]; [yourScrollView addSubview:self.imageView]; yourScrollview.contentSize = yourImageView.bounds.size; [yourScrollView setZoomScale:yourScrollView.bounds.size.width/yourImageView.bounds.size.width animated:NO]; 
+4
source

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


All Articles