2011-11-29 39 views
2

我想歪斜一個UIImage的下部是這樣的:如何扭曲一個UIImage的下部

enter image description here

什麼將最好的辦法是什麼?

+0

我收到的一個建議是將圖像分成兩部分,將其中的一部分歪斜,然後重新組合。 – rob

回答

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