简体   繁体   English

如何倾斜UIImage的下部

[英]How do I skew the lower section of a UIImage

I would like to skew the lower portion of a UIImage like this: 我想这样倾斜UIImage的下部:

在此处输入图片说明

What would the best approach be? 最好的方法是什么?

Here's the solution I came up with. 这是我想出的解决方案。 It divides the image in two and then shears the bottom one: 它将图像分为两部分,然后剪切底部的图像:

CGFloat foldHeightInPercent = 0.9f;
CGFloat totalHeight = 39;
CGRect topImageRect = CGRectMake(0, 0, 50, totalHeight * foldHeightInPercent);

//top image
CGFloat scale = [[UIScreen mainScreen] scale];
CGRect topCropRect = CGRectMake(0, 0, image.size.width * scale, image.size.height * foldHeightInPercent * scale);
CGImageRef topImageRef = CGImageCreateWithImageInRect(image.CGImage, topCropRect);
UIImageView *topImageView = [[UIImageView alloc] initWithFrame:topImageRect];
[topImageView setImage:[UIImage imageWithCGImage:topImageRef]];
CGImageRelease(topImageRef);    
[self.view addSubview:topImageView];

//bottom image
CGRect bottomImageRect = CGRectMake(1, totalHeight * foldHeightInPercent, 50, totalHeight * (1.0f - foldHeightInPercent));
CGFloat yPos = image.size.height * foldHeightInPercent * scale;
CGRect bottomCropRect = CGRectMake(0, yPos, image.size.width * scale, image.size.height * (1.0f - foldHeightInPercent) * scale);
CGImageRef bottomImageRef = CGImageCreateWithImageInRect(image.CGImage, bottomCropRect);
UIImageView *bottomImageView = [[UIImageView alloc] initWithFrame:bottomImageRect];
[bottomImageView setImage:[UIImage imageWithCGImage:bottomImageRef]];
CGImageRelease(bottomImageRef);  

CGFloat skewAngle = 20.0f;
CGFloat skew = tan(skewAngle * M_PI / 180.f);
CGAffineTransform t = CGAffineTransformMake(1.0, 0.0, skew, 1.0, 0.0, 0.0);
bottomImageView.transform = t;
[self.view addSubview:bottomImageView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM