• Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • Tutorial Videos
  • Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • Tutorial Videos
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk
  • #Advertise

MyCodeTips mycodetips-newlogocopy1

  • Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • Tutorial Videos
IOS

Capturing the screen and saving it as an image file

Capturing the screen and saving it as an image file

Procedure of capturing image of a UIView

Create a Bitmap Context

Get the CALayer object (calayer, for example) through UIView’s layer property

Call CALayer’s renderInContext: with passing the Bitmap Context as the argument, now you got the captured image in the bitmap context

Get a CGImageRef from CGBitmapContextCreateImage( Bitmap Context ) call

Create an UIImage object by calling [UIImage imageWithCGImage: CGImageRef ]

Call UIImagePNGRepresentation( UIImage object) to get a PNG data (NSData * type)

Save the PNG data to a file

To save a JPEG image, call UIImageJPEGRepresentation() instead of UIImagePNGRepresentation()

Data flow:

UIWindow -> CALayer -> Bitmap Context -> CGImageRef -> UIImage -> NSData * -> PNG/JPEG file

Example code:

NSData *imageData = UIImagePNGRepresentation(capturedImage);

if(imageData != nil)

{

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsPath = [paths objectAtIndex:0];

NSString *filePath = [documentsPath stringByAppendingPathComponent:@”myfile.png”];

[imageData writeToFile:filePath atomically:YES];

}

EDIT: The procedure above generates corrupted pixels when there is transparent pixels (not fully opaque, through alpha value). Instead, following piece of code works better and even simpler.

+ (UIImage *) captureImageOfView:(UIView *)srcView

{

UIGraphicsBeginImageContext(srcView.bounds.size);

[srcView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *anImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return anImage;

}

However, there is report the smilar code capturing image of the UIWindow object, that represents the whole screen, still corrupts some part of the image, according this link: http://stackoverflow.com/questions/1902741/captured-uiview-image-has-distorted-colors-iphone-sdk

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

[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *anImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

So, it is strongly recommended to capture the entire screen using UIGetScreenImage().

Capture screenshot:

CGImageRef cgImg=UIGetScreenImage();

UIImage *scImage=[UIImage imageWithCGImage:cgImg];

CFRelease(cgImg);

Saving image to “Saved Photo” album

UIImageWriteToSavedPhotosAlbum

Adds the specified image to the user’s Saved Photos album.

void UIImageWriteToSavedPhotosAlbum (

UIImage *image,

id completionTarget,

SEL completionSelector,

void *contextInfo

);

Liked it? Take a second to support Ranjan on Patreon!
become a patron button
  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 4550 Views
Tags | Image, IOS
AUTHOR
Ranjan

Namaste, My name is Ranjan, I am a graduate from NIT Rourkela. This website is basically about of what i learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures, its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

mycodetips-swift

Evolutions of Swift Programming Language

November 1, 2020
objective-c-control-statements2

Objective-C control statements and loops !

October 21, 2021
mycodetips email validation

How to validate email on textField in IOS

February 3, 2020
Next Post
Previous Post

Support us

mycodetips mycodetips
Follow us @ LinkedIn 2850+

Subscribe for updates

Join 8,213 other subscribers

Latest Posts

  • YT-Featured-solidprinciples
    SOLID Principles of Software Design
  • IOS 16 Features
    Latest features in IOS 16
  • r-language
    How can R language be used for data analysis?
  • wordpress-coding-blog
    Guide To WordPress Coding Standards
  • YT-Featured-Algorithm
    What is Algorithm?
  • Frameworks of IOS
    Frameworks of IOS – Part ( I )
  • NSFileManager or NSPathUtilities
    NSFileManager or NSPathUtilities in Objective-C
  • Passing data between view controllers in Objective-C
    Passing data between view controllers in Objective-C
  • structures-classes-enum
    Structures and Classes in swift !
  • control-system-swift
    Control Flow in Swift
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #Guestpost
  • #myQuestions

Android Android Studio API APP Programming Apps blogging CSS DATABASE dsa Features HTML HTML5 installation Interview Questions IOS iPhone javascript Mac objective-c OS Programming quicktips SDK SEO SQL swift Tips & Tricks Tools UI Web Wordpress Xcode

  • SOLID Principles of Software Design
  • Latest features in IOS 16
  • How can R language be used for data analysis?
  • Guide To WordPress Coding Standards
  • What is Algorithm?

©mycodetips.com