【发布时间】:2011-11-29 17:33:55
【问题描述】:
我想像这样倾斜 UIImage 的下部:
最好的方法是什么?
【问题讨论】:
-
我收到的一个建议是将图像分成两部分,倾斜其中一个,然后重新组合。
标签: ios uiimage calayer quartz-2d
我想像这样倾斜 UIImage 的下部:
最好的方法是什么?
【问题讨论】:
标签: ios uiimage calayer quartz-2d
这是我想出的解决方案。它将图像一分为二,然后剪切底部的一个:
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];
【讨论】: