Screenshot method after iOS development diary 19-7.0, screenshot of ios diary 19-7.0

Source: Internet
Author: User

Screenshot method after iOS development diary 19-7.0, screenshot of ios diary 19-7.0

Today, the blogger has a requirement for screenshots and has encountered some difficulties. I would like to share with you the hope that we can make common progress.

IOS7.0 and later abolished the commonly used screenshot method, and also added the screenshot API. The code is relatively simple. Today we will post it for your own research.

1.

-(Void) screenShot

{

UIGraphicsBeginImageContext (self. view. bounds. size );

[Self. view. layer renderInContext: UIGraphicsGetCurrentContext ()];

UIImage * image = UIGraphicsGetImageFromCurrentImageContext ();

UIGraphicsEndImageContext ();

NSLog (@ "image: % @", image );

UIImageView * imaView = [[UIImageView alloc] initWithImage: image];

ImaView. frame = CGRectMake (0, 0,100,100 );

[Self. view addSubview: imaView];

UIImageWriteToSavedPhotosAlbum (image, self, nil, nil );

}

2.

-(Void) srceedShot2

{

// Two seconds of storage delay

Dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (1.0 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {

// Obtain the image Context

// UIGraphicsBeginImageContext (self. view. frame. size );

UIGraphicsBeginImageContext (self. view. frame. size );

// Draw the view to the graph Context

// [Self. view. layer renderInContext: UIGraphicsGetCurrentContext ()];

[Self. view. layer renderInContext: UIGraphicsGetCurrentContext ()];

// Save the screenshot to the album

UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();

UIImageWriteToSavedPhotosAlbum (newImage, self, @ selector (image: didFinishSavingWithError: contextInfo :), nil );

});}

 

 

// Callback after saving to the album

-(Void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo

{

NSString * msg = nil;

If (error ){

Msg = @ "failed to save the image ";

} Else {

Msg = @ "image saved ";

}

UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Save image result prompt"

Message: msg

Delegate: self

CancelButtonTitle: @ "OK"

OtherButtonTitles: nil];

[Alert show];

}

3.

-(Void) screenShot3

{

CGSize size = self. view. bounds. size;

Uigraphicsbeginimagecontextwitexceptions (size, NO, [UIScreen mainScreen]. scale );

CGRect rec = CGRectMake (self. view. frame. origin. x, self. view. frame. origin. y, self. view. bounds. size. width, self. view. bounds. size. height );

[Self. view drawViewHierarchyInRect: rec afterScreenUpdates: YES];

UIImage * image = UIGraphicsGetImageFromCurrentImageContext ();

UIGraphicsEndImageContext ();

NSData * data = UIImagePNGRepresentation (image );

NSString * path = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES). lastObject;

NSString * filename = [path stringByAppendingPathComponent: @ "foo.png"];

[Data writeToFile: filename atomically: YES];

NSLog (@ "************* % @", filename );

}

 

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.