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:
Screen of preview so you're sure it's empty:
So the method is working.
Related Topics
How to Remove the Bottom Gap of Uipageviewcontroller
Youtube Video Autoplay Inside Uiwebview
How to Evaluate the String Equation in iOS
How to Convert Ciimage to Uiimage in Swift 3.0
How to Disable Qlpreviewcontroller Print Button
Detecting iOS Version Number from User Agent Using Regular Expressions
Why Self.Locationmanager Stopupdatinglocation Doesn't Stop Location Update
Avplayer Uitapgesturerecognizer Not Working
Check If 3D Touch Is Supported and Enabled on the iOS9 Device
iOS Web App: Showing Content Only If the Application Is Standalone
Custom Font Not Working in Wkwebview Swift
Game Center Login Dialog Not Shown Again After Cancelling It for the First Time (Ios7)
How to Launch My Settings Bundle from My Application