Take screenshot of host app using iOS share/action extensions? - swift

I will like to know how to take a screenshot of the iOS host app with the use of a share/action extension.
My use case is as follows:
use the Safari browser to access a webpage (https such as gmail)
tap on the Share button and select the extension
the extension will take a screenshot of the current webpage
An working example for this use case is the Awesome Screenshot iOS app.
I had tried the following methods
reload the baseURI (loadRequest) on a UIWebView/WKWebkit (would not be able to access https contents such as Gmail). So not doing any reload (Awesome Screenshot is not doing a reload btw)
Used the ExtensionPreprocessingJS to obtain the DOM contents through the arguments.completionFunction function. I could not extract the document.body here although i could get the source etc. loadHTMLString with the baseURI will mess up the presentation layer.
Used html2canvas in the ExtensionPreprocessingJS to obtain an image and appended it to the host app's webpage as a child but do not know how to extract it. Also, the image got lost for some webpages such as Gmail.
Mobile Safari does not have the visibleContentsAsDataURL method.
I think a viable approach will be to use the html2canvas in the ExtensionPreprocessingJS but how do I save this image somehow?

Edit: So the below works in the Simulator but does not work on the device. I'm presently looking for a solution as well.
Here's a solution that I think the Awesome Screenshot app uses:
func captureScreen() -> UIImage
{
// Get the "screenshot" view.
let view = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
// Add the screenshot view as a subview of the ShareViewController's view.
self.view.addSubview(view);
// Now screenshot *this* view.
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Finally, remove the subview.
view.removeFromSuperview()
return image
}

Related

I dont know why I cant download this image from: http://upload.wikimedia.org/wikipedia/de/d/d2/Juventus_Turin.svg

I can download images just fine but that URL dont work, is there a known problem with that type of URL?
http://upload.wikimedia.org/wikipedia/de/d/d2/Juventus_Turin.svg
You can download like this:
Create WKWebView
Connect outlet
Conform WKUIDelegate in your controller
Create request with your wiki link
Set webView uiDelegate to self
Load from request
You should configure content for UI :)
Svg images doesn't get directly supported in iOS apps. you simply need to add on a third party pod extension in order to use the svg images in app.
For example : https://github.com/pocketsvg/PocketSVG
Try using this PocketSVG.

Customize AWS Amplify authentication UI for iOS

I'm trying to customize the logo on the signIn page using the following:
AWSMobileClient.sharedInstance()
.showSignIn(navigationController: self.navigationController!,
signInUIOptions: SignInUIOptions(
canCancel: false,
logoImage: UIImage(named: "MyCustomLogo"),
backgroundColor: UIColor.black)) { (result,
err) in
//handle results and errors
}
However, the default logo still appears but the background does turn black. I have MyCustomLogo in the project but it just does not take. I'm fairly new to Apple development so I'm sure I'm missing something simple or maybe I'm not. I just need help pleaseĀ :)
I am able to add a custom logo to my Amplify Drop-In UI using the iOS SDK. You have to be sure to add your logo assets in the Assets.xcassets folder for your project to know where to look and override. Also, I presume you named your logo MyCustomLogo as you show in your code block above. You don't need to specify .png.

SDWebImage clearing cache

I'm displaying a list of icons downloaded from the web with text in a table view. The icons can be changed on server side and I need to replace them as soon as new icons are getting available. I try using the following code:
[imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"table_avatar_icon"] options:SDWebImageCacheMemoryOnly];
And call [[SDImageCache sharedImageCache] clearMemory]; In my refresh callback, but it does not purge the contents of the cache. More to it, even if I close the application and open it again the image is still there.
I found only one way to clear the cache and it is by calling [[SDImageCache sharedImageCache] clearDisk];. Which only works after I close and reopen the app.
How can I force SDWebImage to not to use disk caching?
SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];
Don't forget to put these lines of code in your didReceiveMemoryWarning, too.
Located the source if an issue. It seems that I was deceived by the Singleton pattern used in SDImageCache. The cache for extension that is used over UIImageView is being controlled by SDWebImageManager which has an instance variable of SDImageCache. If you want to clear the cache for extension you have to call its imageCache's methods like clearDisk and clearMemory.
Only following code worked for me : Swift 5.0, Xcode 11, iOS 13, SDWebImage pod 5.0
SDWebImageManager.shared.imageCache.clear(with: .all) {
print("deleted all")
}
where you choose options like SDImageCacheType.disk, SDImageCacheType.memory, SDImageCacheType.disk
Also if you want to remove specific image from cache use following:
SDWebImageManager.shared.imageCache.removeImage(forKey: "url of image", cacheType: .all)
Except SDImageCache methods, I strongly advise you to check your image urls. In my situation I tried every method for imageCache and memory issue was still continue. Crashes were occur mainly on iPhone 4s because of hardware it couldn't handle it.
Main issue was url ampersand encoding!
In example, check out these urls: first url is using "&amp" and second one is not. Because of ampersand my JSON library can't read the width and width value get much higher then it should be. That is why I had a memory issue.
1) /select.php?imageid=101961221 "&amp" ;width=100 "&amp" ;isWatermarked=true
2) /select.phpimageid=101961221&width=100&isWatermarked=true
Also the latest versions of SDWebImage library has include UIImageView+WebCache.h class and it really nicely handle cache problems.
The icons can be changed on server side,
so you need to load with refresh cached every time.
if you're using latest SDWebImage framework (5.12.x)
you can call it like this,
[imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:#"xxx" options:SDWebImageRefreshCached];
edit at 220112.

How to Bind Load event for Image in jQueryMobile

I have a mobile website running jQuery Mobile 1.0a2 and I'm currently testing in Mobile Safari for Firmware 4.1 via the iPhone Simulator.
I cannot seem to bind to the load event for an image.
I have a simple gallery of thumbnails and a large image. When you click on a thumbnail it changes the src attribute of the main img
The js for that uses the live('click' method to bind and it works just fine.
$('.gallery-navigation img').live('click',function() {
// change source of main image to new
$.mobile.pageLoading(); // show jquerymobile loading message
});
My problem is that I need feedback on this click, so I wanted to show a loading message (provided by jquerymobile) and then hide it once the image loads.
Where #gallery_image_large is the actual <img> where the src is changing, I tried the following:
$("#gallery_image_large").bind("load", function () {
$.mobile.pageLoading(true); // hide jquerymobile loading message
});
This works in Safari on my desktop but does not in the iPhone Simulator mentioned above.
For reference:
jQuery Mobile Loading Message Methods
UPDATE: I am experimenting with JQuery Image load fails on MobiOne iPhone simulator, which explains how to implement the .load manually by "checking .complete".
I changed the structure of my jquery and it's seemed to have fixed it!
$('#gallery_image_large').one('load',function() {
try { // a grade
$.mobile.pageLoading(true);
} catch(err) { // low grade
}
}).attr('src',full_src);
(as you can see, I opt'd for a try { .. } catch { .. } to verify jquerymobile is available.
Although I didn't use the solution (directly) from JQuery Image load fails on MobiOne iPhone simulator, the manual firing of load via .complete is probably a good solution for anyone else out there!

Updating UIWebView off screen?

I'm trying to get a UIImage of a UIWebView, I can do this fine using renderInContext:. However before I generate the image I would like to update the UIWebView with some HTML code, the trick is the UIWebView is hidden. Right now, I can not get the updated version of the web view to render in the UIImage, just the old one.
Are you waiting until the UIWebView re-renders before querying it again? Loading HTML into a UIWebView does not immediately update the view. You need to wait for webViewDidFinishLoad:. This is true even if the HTML is a simple string with no external references.