UIImageView content mode

ぐ巨炮叔叔 提交于 2019-12-23 07:29:07

问题


The blue line is the imageview's bounds. UIImageView's contentMode is UIViewContentModeScaleAspectFit, And I want keep the original picture's scale. How can I make the picture's left edge is on the UIImageView's left edge? But not like UIViewContentModeTopLeft mode. Keep the scale and no blank in the left. My english is not good, hope you guys could understand what I'm saying. Thank you very much.


回答1:


 yourimageView.contentMode = UIViewContentModeCenter;
if ( yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height) {
   yourimageView.contentMode = UIViewContentModeScaleAspectFit;
}

Swift3

yourimageView.contentMode = .center
if yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height {
yourimageView.contentMode = .scaleAspectFit
}

you can change your mode as

typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};



回答2:


the moment i saw your situation,i thought about this:

_imageView.contentMode = UIViewContentModeLeft | UIViewContentModeScaleAspectFit;

and i tried it, but it makes things worse

so i think you may resize your imageView`s bound to fit the size you want with this code

/// resize image to fit in show area without changing image ratio
+ (CGSize)resizeImageViewFromImageSize:(CGSize)imageSize toFitShowSize:(CGSize)showSize {
CGFloat ratioW = imageSize.width / showSize.width;
CGFloat ratioH = imageSize.height / showSize.height;

CGFloat ratio = imageSize.width / imageSize.height;

if (ratioW > 1 && ratioH > 1) { 

    if (ratioW > ratioH) { 
        imageSize.width = showSize.width;
        imageSize.height = imageSize.width / ratio;
    } else {
        imageSize.height = showSize.height;
        imageSize.width = imageSize.height * ratio;
    }

} else if (ratioW > 1) {

    imageSize.width = showSize.width;
    imageSize.height = imageSize.width / ratio;

} else if (ratioH > 1) {

    imageSize.height = showSize.height;
    imageSize.width = imageSize.height * ratio;

} else {

    if (ratioW > ratioH) { 

        if (showSize.width / imageSize.width <= 2) {   
            imageSize.width = showSize.width;
            imageSize.height = imageSize.width / ratio;
        }

    } else {

        if (showSize.height / imageSize.height <= 2) {
            imageSize.height = showSize.height;
            imageSize.width = imageSize.height * ratio;
        }

    }

}

return imageSize;
}

then put your imageView align to left of the screen




回答3:


just like this

imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > imageView.size.width && imageView.bounds.size.height > imageView.size.height) {
   imageView.contentMode = UIViewContentModeScaleAspectFit;
}



回答4:


Swift

import UIKit
import AVFoundation



let originalsize = CGSizeMake(500, 500)
let rect = CGRectMake(0, 0, 320, 480)


var result = AVMakeRectWithAspectRatioInsideRect(originalsize, rect)

print(result)


来源:https://stackoverflow.com/questions/26400382/uiimageview-content-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!