0

In my application, i am sending my desktop screenshot to web service by converting it to base64 string. Here is the code to obtain the screenshot.

CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault); 

Now i want to convert this screenshot to base64 string format. Can anyone provide the solution to convert my screenshot to base64 string format.

Note: conversion from NSImage/NSData to base64 string format also acceptable.

santhosh
  • 1,191
  • 4
  • 14
  • 28
  • I don't know this enough to provide a full answer, but after a quick peek at the docs I can poke you in the direction of `CGImageGetDataProvider()` and `CGImageGetBitmapInfo()`, which might give you what you need to start out. For NSImage you might have an easier time if you need a specific file format using NS(Bitmap)ImageRep. – andlabs Sep 03 '14 at 23:11
  • @andlabs I will check with this reference to change the file format. – santhosh Sep 04 '14 at 13:06

1 Answers1

0

Here is my implementation to convert CGImageRef to base64 string.

CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault); 

NSBitmapImageRep * bitmapImageRep = [[NSBitmapImageRep alloc]initWithCGImage:screenShot];

NSImage *image = [[NSImage alloc] init];

[image addRepresentation:bitmapImageRep];

NSData *imgData = [image TIFFRepresentation];

NSString *encodedString = [self base64encde: imgData];

// Encoding base64

- (NSString*)base64encde:(NSData*)input
{
    SecTransformRef transform = SecEncodeTransformCreate(kSecBase64Encoding, NULL);

    return [[NSString alloc] initWithData:base64helper(input, transform) encoding:NSASCIIStringEncoding] ;
}
santhosh
  • 1,191
  • 4
  • 14
  • 28