[Ios] ios-Demo4 undress/scratch prize app-Professional, ios-demo4app-

Source: Internet
Author: User

[Ios] ios-Demo4 undress/scratch prize app-Professional, ios-demo4app-

However, only CGContextClearRect In the Ios system does not have the o method. I checked some information online. It is found that Mask in CG is used for implementation.
:


This effect can be used to customize the width of the paint brush and other related settings. It does not appear as serious as in normal mode.

The specific analysis is as follows:
1. Obtain the CGImageRef of the layer for which you want to hide the View.
2. Open the CFMutableDataRef space that meets the 1 CGImageRef to use the BitmapContext mask below.
3. Obtain CGDataProviderRef through CFMutableDataRef (used by maskCreate)
3. BitmapContextCreate
4. Fill BitmapContext in black and set the paint brush (white, width, line cap ))
5. When CGImageMaskCreate passes in CGDataProviderRef, CGImageRef points to the BitmapContext created just now. If BitmapContext is changed, the content of CGImageRef also changes.
6. CGImageCreateWithMask-create CGImageRef using CGImageRef of Mask. CGImageRef changes with CGImageRef of Maskde (So BitmapContext changes CGImageRef after Mask changes the mask Mask)
7. Release

8. When you touch your finger, add and draw paths to bitmapContext based on the previous CGPoint and current CGPoint. Call self setNeedsDisplay and re-obtain CGImageRef after mask to obtain the latest UIImage.
9. Upload the latest UIImage drawRect to the interface.


Code:
//
// PLLScrathView. h
// PLLScratchViewDemo
//
// Created by liu poolo on 14-7-31.
// Copyright (c) 2014 liu poolo. All rights reserved.
//

# Import <UIKit/UIKit. h>
# Import <QuartzCore/QuartzCore. h>

@ Interface PLLScratchView: UIView
@ Property (nonatomic, assign) float sizeBrush;
-(Void) setHideView :( UIView *) hideView;
@ End

//
// PLLScrathView. m
// PLLScratchViewDemo
//
// Created by liu poolo on 14-7-31.
// Copyright (c) 2014 liu poolo. All rights reserved.
//

# Import "PLLScratchView. h"
@ Interface PLLScratchView (){
CGContextRef _ contextMask; // The context changed by the maskContext user touch
CGImageRef _ scratchCGImg; // CGimageRef encapsulation _ contextMask Image Information _ contextMask changes and changes until the call generates a UIImage
CGPoint currPonit;
CGPoint prePoint;
}
@ End

@ Implementation PLLScratchView
@ Synthesize sizeBrush;
-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
[Self setOpaque: NO];
// Set transparency. If it is not transparent, you cannot see the next layer.
Self. sizeBrush = 10.0f;
}
Return self;
}

-(Void) drawRect :( CGRect) rect
{
[Super drawRect: rect];
UIImage * imageToDraw = [UIImage imageWithCGImage: _ scratchCGImg];
[ImageToDraw drawInRect: self. frame];
}
// SetSizeBrush before setHideView
-(Void) setHideView :( UIView *) hideView {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray ();
CGFloat scale = [UIScreen mainScreen]. scale;

// Obtain the CGImage of the currently passed View
Uigraphicsbeginimagecontextwitexceptions (hideView. bounds. size, NO, 0 );
HideView. layer. contentsScale = scale;
[HideView. layer renderInContext: UIGraphicsGetCurrentContext ()];
CGImageRef hideCGImg = UIGraphicsGetImageFromCurrentImageContext (). CGImage;
UIGraphicsEndImageContext ();

// Draw the Bitmap mask
Size_t width = CGImageGetWidth (hideCGImg );
Size_t height = CGImageGetHeight (hideCGImg );

CFMutableDataRef pixels;
Pixels = CFDataCreateMutable (NULL, width * height );
// Create a variable dataRef for storing records in bitmap
_ ContextMask = CGBitmapContextCreate (CFDataGetMutableBytePtr (pixels), width, height, 8, width, colorSpace, kCGImageAlphaNone );

// Data provider
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData (pixels );

// Fill in the black background. The black range in the mask is displayed. The white color is not displayed.
CGContextSetFillColorWithColor (_ contextMask, [UIColor blackColor]. CGColor );
CGContextFillRect (_ contextMask, self. frame );

CGContextSetStrokeColorWithColor (_ contextMask, [UIColor whiteColor]. CGColor );
CGContextSetLineWidth (_ contextMask, self. sizeBrush );
CGContextSetLineCap (_ contextMask, kCGLineCapRound );

CGImageRef mask = CGImageMaskCreate (width, height, 8, 8, width, dataProvider, nil, NO );
_ ScratchCGImg = CGImageCreateWithMask (hideCGImg, mask );

CGImageRelease (mask );
Cgcolorspacerelstrap (colorSpace );

}

-(Void) scratchViewFrom :( CGPoint) startPoint toEnd :( CGPoint) endPoint {
Float scale = [UIScreen mainScreen]. scale;
// Y of CG and y0 of UI are opposite. in the upper left corner, CG is in the lower left corner.
CGContextMoveToPoint (_ contextMask, startPoint. x * scale, (self. frame. size. height-startPoint.y) * scale );
CGContextAddLineToPoint (_ contextMask, endPoint. x * scale, (self. frame. size. height-endPoint.y) * scale );
CGContextStrokePath (_ contextMask );
[Self setNeedsDisplay];
}

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
[Super touchesBegan: touches withEvent: event];
}


-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {
[Super touchesMoved: touches withEvent: event];
UITouch * touch = [touches anyObject];
CurrPonit = [touch locationInView: self];
PrePoint = [touch previuslocationinview: self];
[Self scratchViewFrom: prePoint toEnd: currPonit];
}

-(Void) toucheseEnd :( NSSet *) touches withEvent :( UIEvent *) event {
[Super touchesEnded: touches withEvent: event];
UITouch * touch = [touches anyObject];
CurrPonit = [touch locationInView: self];
PrePoint = [touch previuslocationinview: self];
[Self scratchViewFrom: prePoint toEnd: currPonit];
}


-(Void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event {
[Super touchesCancelled: touches withEvent: event];
}


@ End

 

  • The size is 566.6 KB.
  • PLLScratchView.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.