0

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

在此处输入图像描述

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

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

4

1 回答 1

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;

}

我相信它会帮助你。

于 2013-08-06T07:49:57.610 回答