有没有一种方法可以为具有宽高比填充的UIImage定义自定义偏移量?
因此,当前方面填充会缩放图像并将其居中。
但是,我需要一种定义偏移量的方法,以便可以手动调整图像以使其更合适。
这可能吗?我查看了UIImage类,但找不到任何东西。
我看不到使用UIImageView单独执行此操作的方法,但是如果将其放在一个包含视图中,将其设置为宽高比,然后更改该包含视图的边界,则可以获得所需的效果。这是我的操作方式:
UIView *containingView = [[UIView alloc] initWithFrame:CGRectInset(self.view.bounds, 10, 10)];
containingView.clipsToBounds = YES;
[self.view addSubview:containingView];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.frame = containingView.bounds;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[containingView addSubview:imageView];
// Offset the image 100 pixels to the left
containingView.bounds = CGRectMake(100.0f, 0.0f, CGRectGetWidth(containingView.bounds), CGRectGetHeight(containingView.bounds));
Run Code Online (Sandbox Code Playgroud)
确保imageView的clipsToBounds设置为NO(默认值),以便您可以更改containsView.bounds来平移将被裁剪掉的图像部分。
SWIFT 4+
let containerView = UIView(frame: view.bounds)
containerView.clipsToBounds = true
view.addSubview(containerView)
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.frame = containerView.bounds
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
containerView.addSubview(imageView)
containerView.bounds = CGRect(x: imageXOffset,
y: imageYOffset,
width: containerView.bounds.width,
height: containerView.bounds.height)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1288 次 |
| 最近记录: |