混合两个图像并从两个UIImageViews中绘制已调整大小的图像

时间:2012-06-28 21:38:21

标签: objective-c ios xcode cocoa-touch uiimageview

我有两个ImageView,一个叫做imageView,另一个叫做subView(是imageView的子视图)。

我想将这些视图上的图像混合在一起,用户可以使用平移来切换混合的alpha。我的代码有效,但是现在,代码很慢,因为每次移动平移手势时我们都会重新绘制图像。有更快/更有效的方法吗?

BONUS问:我想允许放大我的子视图图像。目前我已经将我的subView设置为UIViewContentModeCenter,但是我似乎无法使用此内容模式绘制我的图像的放大部分。有没有办法解决这个问题?

我的直截了当:

- (void)drawRect:(CGRect)rect
{
float xCenter = self.center.x - self.currentImage1.size.width/2.0;
float yCenter = self.center.y - self.currentImage1.size.height/2.0;

subView.alpha = self.blendAmount;  // Customize the opacity of the top image.

UIGraphicsBeginImageContext(self.currentImage1.size);

CGContextRef c = UIGraphicsGetCurrentContext();

CGContextSetBlendMode(c, kCGBlendModeColorBurn);
[imageView.layer renderInContext:c];

self.blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.blendedImage drawAtPoint:CGPointMake(xCenter,yCenter)];

}

1 个答案:

答案 0 :(得分:1)

您需要使用GPU进行图像处理,这比使用CPU要快得多(正如您现在所做的那样)。

您可以使用Core Image框架,该框架非常快速且易于使用但需要iOS 5,或者您可以直接使用Open GL,但您需要具备经验并且对Open GL着色有一定的了解。