gpt4 book ai didi

iphone - 如何在不拉伸(stretch)中心的情况下拉伸(stretch) UIImageView?

转载 作者:技术小花猫 更新时间:2023-10-29 10:24:14 27 4
gpt4 key购买 nike

enter image description here

我想拉伸(stretch)上图的左右两边,中间的箭头不拉伸(stretch)。我该怎么做?

最佳答案

如果你不想打扰 Photoshop 并且如果图像不需要动态调整大小,你可以在 UIImage 上使用我的类别方法。

名为“17maA.png”的样本图片宽度为 124 像素。因此,您可以轻松地创建一个 300 像素宽的版本:

UIImage *image = [UIImage imageNamed:@"17maA"];
UIImage *newImage = [image pbResizedImageWithWidth:300 andTiledAreaFrom:10 to:20 andFrom:124-20 to:124-10];

这是分类方法:

- (UIImage *)pbResizedImageWithWidth:(CGFloat)newWidth andTiledAreaFrom:(CGFloat)from1 to:(CGFloat)to1 andFrom:(CGFloat)from2 to:(CGFloat)to2  {
NSAssert(self.size.width < newWidth, @"Cannot scale NewWidth %f > self.size.width %f", newWidth, self.size.width);

CGFloat originalWidth = self.size.width;
CGFloat tiledAreaWidth = (newWidth - originalWidth)/2;

UIGraphicsBeginImageContextWithOptions(CGSizeMake(originalWidth + tiledAreaWidth, self.size.height), NO, self.scale);

UIImage *firstResizable = [self resizableImageWithCapInsets:UIEdgeInsetsMake(0, from1, 0, originalWidth - to1) resizingMode:UIImageResizingModeTile];
[firstResizable drawInRect:CGRectMake(0, 0, originalWidth + tiledAreaWidth, self.size.height)];

UIImage *leftPart = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIGraphicsBeginImageContextWithOptions(CGSizeMake(newWidth, self.size.height), NO, self.scale);

UIImage *secondResizable = [leftPart resizableImageWithCapInsets:UIEdgeInsetsMake(0, from2 + tiledAreaWidth, 0, originalWidth - to2) resizingMode:UIImageResizingModeTile];
[secondResizable drawInRect:CGRectMake(0, 0, newWidth, self.size.height)];

UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return fullImage;
}

关于iphone - 如何在不拉伸(stretch)中心的情况下拉伸(stretch) UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15427317/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com