在 UIImageView 中移动 UIImage

Move UIImage inside UIImageView

我有一个 UIImageView(红色方块),它将显示必须缩放的 UIImage(我可以接收比 UIImageView 更大或更小的图像)。缩放后, UIImage 的显示部分是它的中心。

我需要的是显示图片中蓝色方块的部分,如何存档?

我只能获取图像尺寸(高度和宽度),但它显示的是原始尺寸,而应该是缩放后的尺寸。

self.viewIm = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 120, 80)];
self.viewIm.backgroundColor = [UIColor greenColor];
self.viewIm.layer.borderColor = [UIColor redColor].CGColor;
self.viewIm.layer.borderWidth = 5.0;
UIImage *im = [UIImage imageNamed:@"benjen"];
self.viewIm.image = im;
self.viewIm.contentMode = UIViewContentModeScaleAspectFill;
//    self.viewim.clipsToBounds = YES;
[self.view addSubview:self.viewIm];

尝试这样的事情:

CGRect cropRect = CGRectMake(0,0,200,200);
CGImageRef imageRef = CGImageCreateWithImageInRect([ImageToCrop CGImage],cropRect);
UIImage *image = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

要完成您想要做的事情,我建议您查看 CALayercontentsRect 属性。

自从看到您的回答后,我一直在尝试找出正确的解决方案一段时间,但数学让我逃脱了,因为 contentsRect: 的 x 和 y 参数看起来有点神秘......但这里是一些可能为您指明正确方向的代码...

float imageAspect = self.imageView.image.size.width/self.imageView.image.size.height;
float imageViewAspect = self.imageView.frame.size.width/self.imageView.frame.size.height;

if (imageAspect > imageViewAspect) {
    float scaledImageWidth = self.imageView.frame.size.height * imageAspect;
    float offsetWidth = -((scaledImageWidth-self.imageView.frame.size.width)/2);
    self.imageView.layer.contentsRect = CGRectMake(offsetWidth/self.imageView.frame.size.width, 0.0, 1.0, 1.0);
} else if (imageAspect < imageViewAspect) {
    float scaledImageHeight = self.imageView.frame.size.width * imageAspect;
    float offsetHeight = ((scaledImageHeight-self.imageView.frame.size.height)/2);
    self.imageView.layer.contentsRect = CGRectMake(0.0, offsetHeight/self.imageView.frame.size.height, 1.0, 1.0);
}

我在 this answer 上找到了一个非常好的近似值。其中,类别调整图像大小,然后使用中心点进行裁剪。我将它调整为使用 (0,0) 作为原点进行裁剪。因为我真的不需要类别,所以我将它用作单一方法。

- (UIImage *)imageByScalingAndCropping:(UIImage *)image forSize:(CGSize)targetSize {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetSize.width;
    CGFloat scaledHeight = targetSize.height;

    if (CGSizeEqualToSize(image.size, targetSize) == NO) {
        if ((targetSize.width / image.size.width) > (targetSize.height / image.size.height)) {
            scaleFactor = targetSize.width / image.size.width; // scale to fit height
        } else {
            scaleFactor = targetSize.height / image.size.height; // scale to fit width
        }

        scaledWidth  = image.size.width * scaleFactor;
        scaledHeight = image.size.height * scaleFactor;
    }

    UIGraphicsBeginImageContext(targetSize); // this will crop

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    if(newImage == nil) {
        NSLog(@"could not scale image");
    }

    //pop the context to get back to the default
    UIGraphicsEndImageContext();

    return newImage;
}

我的电话是这样的:

self.viewIm = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 120, 80)];
self.viewIm.image = [self imageByScalingAndCropping:[UIImage imageNamed:@"benjen"] forSize:CGSizeMake(120, 80)];
[self.view addSubview:self.viewIm];

我在这上面花了一些时间并最终创建了一个 Swift 3.2 解决方案(基于我在另一个线程上的一个答案,以及一个以上答案)。此代码仅允许图像的 Y 平移,但通过一些调整,任何人都应该能够添加水平平移;)

let yOffset: CGFloat = 20
myImageView.contentMode = .scaleAspectFill

//scale image to fit the imageView's width (maintaining aspect ratio), but allow control over the image's Y position
UIGraphicsBeginImageContextWithOptions(myImageView.frame.size, myImageView.isOpaque, 0.0)
let ratio = myImage.size.width / myImage.size.height
let newHeight = myImageView.frame.width / ratio
let rect = CGRect(x: 0, y: -yOffset, width: myImageView.frame.width, height: newHeight)
myImage.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() ?? myImage
UIGraphicsEndImageContext()

//set the new image
myImageView.image = newImage

现在您可以通过更改 yOffset.

来调整您需要的图像的上下距离。