Taking Screenshots in the Background (Ios) - Improving Performance

Get a screenshot while App is in background? (Private APIs allowed)

After massive testing and trying, I concluded that even with private APIs erasing the photos from the photo album is too much of a problem, maybe even impossible.
I ended up writing a script that simulates user inputs and erases the photos.

Creating an image out of the ios surface and saving it

I remember I saw something on this subject (taking screenshots in the background) some time ago. I wasn't able find exact place and code which I saw. The only note which I left for myself was usage of createScreenIOSurface

I googled a little bit and found couple of mentions:

Get a screenshot while App is in background? (Private APIs allowed)

https://github.com/rpetrich/FastBlurredNotificationCenter/blob/master/Tweak.x

http://pastie.org/pastes/3734430

As I remember the code was way more compact then what you showed.

Capturing Screenshot of UIVIew with multiple CALayers displays white background while save image in gallery

I've tried your code. You can try following implementation:

- (void)viewDidLoad {
[super viewDidLoad];

UIView *tmpView = [UIView new];
tmpView.frame = self.view.bounds;
tmpView.backgroundColor = [UIColor clearColor];
[self.view addSubview:tmpView];

UIImage *img = [self imageWithView:tmpView];
[self saveInJPGFormat:img];
[self saveInPNGFormat:img];
}

- (void)saveInJPGFormat:(UIImage *)image {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

- (void)saveInPNGFormat:(UIImage *)image {
NSData* imageData = UIImagePNGRepresentation(image);
UIImage* pngImage = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil);
}

- (UIImage *) imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

Your method saves as JPG to camera roll. JPGs aren't capable to keep alpha channel.
Second method taken from https://stackoverflow.com/a/10279075/849616 saves image as PNG. I can open it and I can see through the image (alpha channel is saved).

BTW: ofc that's very dirty and quick code. In reality you should do a category on UIImage for these methods. Also keep MVC and either views&layers stick to storyboards or to separate UIView subclass.

Images from my photo gallery: CameraRoll
Screen of preview so you're sure it's empty: Sample Image

So the method is working.



Related Topics



Leave a reply



Submit