在png图像上拖放颜色

时间:2013-08-06 07:45:41

标签: iphone ios objective-c drag-and-drop uiimageview

在iOS应用程序中。我想在我放置了png图像的UIImageView上拖放图像。我只想在图像中使用这些颜色,而不是在透明区域。附图可以更好地解释我的问题

enter image description here

我想从底部拖动图像并放在图像上,它应该是这样的。

底部的这些颜色可以被视为UIImageViews

1 个答案:

答案 0 :(得分:0)

- (UIImage *) changeColorForImage:(UIImage *)image toColor:(UIColor*)color {
UIGraphicsBeginImageContext(image.size);

CGRect contextRect;
contextRect.origin.x = 0.0f;
contextRect.origin.y = 0.0f;
contextRect.size = [image size];
// Retrieve source image and begin image context
CGSize itemImageSize = [image size];
CGPoint itemImagePosition;
itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) );

UIGraphicsBeginImageContext(contextRect.size);

CGContextRef c = UIGraphicsGetCurrentContext();
// Setup shadow
// Setup transparency layer and clip to mask
CGContextBeginTransparencyLayer(c, NULL);
CGContextScaleCTM(c, 1.0, -1.0);
CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [image CGImage]);
// Fill and end the transparency layer

CGContextSetBlendMode(c, kCGBlendModeColorDodge);

CGContextSetFillColorWithColor(c, color.CGColor);

contextRect.size.height = - contextRect.size.height;
contextRect.size.height -= 15;
CGContextFillRect(c, contextRect);
CGContextEndTransparencyLayer(c);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;

}

我相信它会对你有所帮助。