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 );
}