Picture cut
One, use quartz2d to finish the picture cut
1. Display the picture in a custom view
First draw the picture to the view. Draw the picture to a point according to the original size.
Code:
Copy Code code as follows:
-(void) DrawRect: (cgrect) rect
{
UIImage *image2=[uiimage imagenamed:@ "Me"];
[Image2 drawatpoint:cgpointmake (100, 100)];
}
Show:
2. Cut the picture to let the picture Circle show
Train of thought: Draw a circle first, let the picture show inside the circle, the excess part does not show.
Note: The scope of the display is limited to the specified clipping range, and no matter what is drawn in the context, it is not displayed if it is beyond that range.
Code:
Copy Code code as follows:
-(void) DrawRect: (cgrect) rect
{
Draw a circle so that you can later specify a range of pictures to display
Get Graphics context
Cgcontextref Ctx=uigraphicsgetcurrentcontext ();
Cgcontextaddellipseinrect (CTX, CGRectMake (100, 100, 50, 50));
The scope of the specified context in which the content can be displayed is a circle
Cgcontextclip (CTX);
UIImage *image2=[uiimage imagenamed:@ "Me"];
[Image2 drawatpoint:cgpointmake (100, 100)];
}
Show:
3. Cut the picture and let the picture triangle show
Code:
Copy Code code as follows:
-(void) DrawRect: (cgrect) rect
{
Draw a triangle so that you can specify a range of pictures to display at a later time
Get Graphics context
Cgcontextref Ctx=uigraphicsgetcurrentcontext ();
Cgcontextaddellipseinrect (CTX, CGRectMake (100, 100, 50, 50));
Cgcontextmovetopoint (CTX, 100, 100);
Cgcontextaddlinetopoint (CTX, 60, 150);
Cgcontextaddlinetopoint (CTX, 140, 150);
Cgcontextclosepath (CTX);
Note: Specify the range (that is, the method that specifies the cut must be called before the range is drawn)
The scope of the specified context in which the content can be displayed is a circle
Cgcontextclip (CTX);
UIImage *image2=[uiimage imagenamed:@ "Me"];
[Image2 drawatpoint:cgpointmake (100, 100)];
}
Show:
Screen Cutting
One, simple explanation
In program development, it is sometimes necessary to intercept a piece of content on the screen, such as the game of fishing talent. As shown in figure:
Second, the code example
Storyboard Interface Setup
Code:
Copy Code code as follows:
//
Yyviewcontroller.m
01-Cut Screen
//
Created by Apple on 14-6-12.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYViewController.h"
#import "Mbprogresshud+nj.h"
@interface Yyviewcontroller ()
@property (Weak, nonatomic) Iboutlet UIView *contentview;
-(Ibaction) Btnclick: (UIButton *) sender;
@end
Copy Code code as follows:
@implementation Yyviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
}
-(Ibaction) Btnclick: (UIButton *) Sender {
Delay two seconds to save
Dispatch_after (Dispatch_time (Dispatch_time_now, int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
Get Graphics context
Uigraphicsbeginimagecontext (self.view.frame.size);
Uigraphicsbeginimagecontext (self.contentView.frame.size);
Draw the view into the graphics context
[Self.view.layer Renderincontext:uigraphicsgetcurrentcontext ()];
[Self.contentView.layer Renderincontext:uigraphicsgetcurrentcontext ()];
Save a screenshot to an album
UIImage *newimage=uigraphicsgetimagefromcurrentimagecontext ();
Uiimagewritetosavedphotosalbum (Newimage,self, @selector (image:didFinishSavingWithError:contextInfo:), nil);
});
}
-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo
{
if (Error) {
[Mbprogresshud showerror:@ failed to save, please check for relevant permissions "];
}else
{
[Mbprogresshud showmessage:@] Save success! "];
[Mbprogresshud showsuccess:@] Save success! "];
}
}
@end
Save the captured pictures in your phone's album:
Description: Draw the whole screen into a picture
1. Create a bitmap context
2. Draw the screen with the context
3. Draw a picture from the context
4. Save pictures to albums
Add: The code to write the picture to the file
Copy Code code as follows:
3. Draw a picture from the context
UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();
NSData *data = uiimagepngrepresentation (newimage);
NSString *path = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES] lastObject] stringbyappendingpathcomponent:@ "Abc.png"];
NSLog (@ "%@", Path);
[Data Writetofile:path Atomically:yes];
Third, to supplement
What should you do after saving success and saving failures?
Recommended methods for the system:
Copy Code code as follows:
-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo
{
if (Error) {
[Mbprogresshud showerror:@ failed to save, please check for relevant permissions "];
}else
{
[Mbprogresshud showmessage:@] Save success! "];
[Mbprogresshud showsuccess:@] Save success! "];
}
}
If the picture is saved successfully, then you are prompted to save it successfully.
If the save fails, then the prompt fails
Tip: There are two common reasons for saving failures: 1 is not enough memory, 2 is the internal authority of the phone is not allowed.
Description: If an application wants to access the Address Book or album, the user has explicitly refused, then the next visit will be directly rejected. At this time, you can prompt the user to open permissions.