(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
const float* colors = CGColorGetComponents( color.CGColor ); CGContextSetRGBFillColor(c, colors[0], colors[1], colors[2], .75);
contextRect.size.height = -contextRect.size.height; contextRect.size.height -= 15; CGContextFillRect(c, contextRect); CGContextEndTransparencyLayer(c);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; }
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view == imgBack) { lastPoint = [touch locationInView:imgBack]; }
}
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:imgBack];
UIGraphicsBeginImageContext(imgBack.frame.size); [imgBack.image drawInRect: CGRectMake(0, 0,imgBack.image.size.width, imgBack.image.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.9, 2.0); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); imgBack.image = UIGraphicsGetImageFromCurrentImageContext(); imgBack.contentMode = UIViewContentModeScaleToFill; imgBack.clipsToBounds = YES; UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
I used these two i can fill the images with my colour and on trying to draw on the image it just shows the line i draw but the image got disappeared is there any way to combine both. i.e i should fill the image with desired colour and also to draw desired image in the image using touch movement (draw like using pencil on the image). And also give ideas to save the image as a screenshot if possible. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire