如何倾斜UIImage的下半部分

时间:2011-11-29 17:33:56

标签: ios uiimage calayer quartz-2d

我想像这样扭曲UIImage的下半部分:

enter image description here

最好的方法是什么?

1 个答案:

答案 0 :(得分:5)

这是我提出的解决方案。它将图像分成两部分,然后剪下底部图像:

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];