Image clipping and screenshot function in iOS developed quartz2d use

Source: Internet
Author: User

Original http://www.jb51.net/article/75671.htm

Picture clipping
First, the use of quartz2d to complete the picture cut
1. Display the image in a custom view
First, draw the picture onto the view. According to the original size, the picture is drawn to a point.
Code:

Copy CodeThe code is as follows:
-(void) DrawRect: (cgrect) rect
{
UIImage *image2=[uiimage imagenamed:@ "Me"];
[Image2 drawatpoint:cgpointmake (100, 100)];
}


Show:

2. Cut the picture to make the picture appear round
Idea: Draw a circle first, let the picture appear inside the circle, the outside part does not show.

Note: The displayed range is limited to the specified clipping range, and no matter what is drawn in the context, it will not be displayed if the range is exceeded.
Code:

Copy CodeThe code is as follows:
-(void) DrawRect: (cgrect) rect
{
Draw a circle so that you can specify a range of pictures to be displayed later
Get the graphics context
Cgcontextref Ctx=uigraphicsgetcurrentcontext ();
Cgcontextaddellipseinrect (CTX, CGRectMake (100, 100, 50, 50));

Specifies that the range of content that can be displayed in the context is the range of circles
Cgcontextclip (CTX);
UIImage *image2=[uiimage imagenamed:@ "Me"];
[Image2 drawatpoint:cgpointmake (100, 100)];
}


Show:

3. Cut the picture to show the picture triangle
Code:

Copy CodeThe code is as follows:
-(void) DrawRect: (cgrect) rect
{

Draw a triangle so you can specify a range to display the picture later
Get the 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 drawing the range)
Specifies that the range of content that can be displayed in the context is the range of circles
Cgcontextclip (CTX);
UIImage *image2=[uiimage imagenamed:@ "Me"];
[Image2 drawatpoint:cgpointmake (100, 100)];
}


Show:

Screen Cutting
A simple explanation

In program development, it is sometimes necessary to intercept a piece of content on the screen, such as a fishing talent game.

Second, code example

Storyboard Interface Setup

Code:

Copy CodeThe code is as follows:
//
Yyviewcontroller.m
01-screenshot
//
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 CodeThe code is as follows:
@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];
}

-(Ibaction) Btnclick: (UIButton *) Sender {

Two seconds delay Save
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
Get the graphics context
Uigraphicsbeginimagecontext (self.view.frame.size);
Uigraphicsbeginimagecontext (self.contentView.frame.size);
Drawing a 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:@ "Save failed, please check if you have the relevant permissions"];
}else
{
[Mbprogresshud showmessage:@ "saved successfully! "];
[Mbprogresshud showsuccess:@ "saved successfully! "];
}
}

@end


Save the captured image to your phone's album:

Description: Draw the entire screen into a picture
1. Create a bitmap context
2. Draw the screen in the context
3. Remove the drawn picture from the context
4. Save pictures to albums
Add: Code that writes a picture to a file

Copy CodeThe code is as follows:
3. Remove the drawn 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, supplementary
What should I do after saving success and saving failures?
System Recommended Method:

Copy CodeThe code is as follows:
-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo
{
if (Error) {
[Mbprogresshud showerror:@ "Save failed, please check if you have the relevant permissions"];
}else
{
[Mbprogresshud showmessage:@ "saved successfully! "];
[Mbprogresshud showsuccess:@ "saved successfully! "];
}
}


If the picture is saved successfully, the prompt is saved successfully.
If the save fails, then the prompt fails
Tip: There are two common reasons for saving failures: 1 is insufficient memory, and 2 is not allowed in the phone's internal permissions.
Note: If an application wants to access the Address Book or album, the user has explicitly rejected it, then the subsequent visit will be rejected directly. This time, you can prompt the user to open permissions.

Image clipping and screenshot function in iOS developed quartz2d use

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.