When loading the pdf in UIWebview it loads properly at first.But when zoomed in and out for sometime, the pdf is not rendered properly. ie in certain zoom level some of the tile area [Rectangular area] appears black and it persist in the same state afterwards.
Please see the image below
Any idea how to make this proper ?
I face same issue like this, i tried to read local pdf using WKWebview and UIWebView. Somehow after I zoom in and zoom out in iPad the view become black like your view and I think its because memory issue and there is problem when rendering the html.
If you want to open pdf better just call UIDocumentInteractionController or PDFKit for above iOS 11
func showPDF(){
let documentController = UIDocumentInteractionController.init(url: localUrl!)
documentController.delegate = self
documentController.presentPreview(animated: true)
}
And you need implement the delegate method
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
Related
I user wkwebview to load a local pdf file and it works fine. But when I scroll up and down to some pages, some pages become black. Have you also come across this problem?
This one works for me.
Assuming you are implementing WKNavigationDelegate (YourWebView.navigationDelegate) in your ViewController.
- (void)webView:(WKWebView *)webView
didFinishNavigation:(WKNavigation *)navigation
{
UIView *v = webView;
while (v) {
v.backgroundColor = [UIColor whiteColor];
v = [v.subviews firstObject];
}
}
For reference: Rendering PDF in UIWebView iOS 8, causes a black border around PDF
For iOS 11.0+ use PDFView (also see PDFKit) to display a PDF. PDFView does not have the issue with black pages.
I need to Disable zooming for UIWebview iOS for swift 2.2.
I am trying like this:
self.webView.scrollView.maximumZoomScale = 1.0;
self.webView.scrollView.minimumZoomScale = 1.0;
But this is not helping. Is there any other way I can disable zoom while the page loads for the first time?
This works for me:
let scrollView = webView.subviews.objectAtIndex(0)
scrollView.delegate = self
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return someView
}
UPDATE: For iOS 5 or higher get scrollView like this:
webview.scrollView.delegate = self;
You can try this also:
use 'webView.scrollView.zoomScale' instead of 1.0, and do it inside the 'webViewDidFinishLoad' (remember to set the web view delegate)
Turn off scalesPageToFit for the webView and set the zoom level manually for the first time loading. SO Post
Hope this helps.
My App has one ViewController, one UIWebView and 4 UIImageView
The control flow in my app:
-> Capture a Image from camera and store into UIImage variable, this happens in didFinishPickingMediaWithInfo function. Do some processing on the Image.
-> From didFinishPickingMediaWithInfo function, load UIWebView with an inline html/css code i.e.
webview.hidden = false
WebView.loadHTMLString(htmlString, baseURL: nil)
-> After the above html is loaded into UIWebView, delegate function webViewDidFinishLoad is called. From webViewDidFinishLoad, take a snapshot of whatever is loaded in UIWebView into a Image with the code below:
var image:UIImage?
autoreleasepool{
UIGraphicsBeginImageContextWithOptions(WebView.frame.size, false, 1.0)
WebView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
-> Store the captured image into a UIImage variable.
-> Load one more different html/css code into the same UIWebView ( I am still in the webViewDidFinishLoad function): the html loading code is same as above i.e.
WebView.loadHTMLString(htmlString1, baseURL: nil)
-> webViewDidFinishLoad is called again because of new html code loaded in above step, and I do the same thing i.e. take a snapshot of the UIWebView content into UIImage variable and load new html pattern
-> I do this 4 times i.e. in the end I have 4 Images captured in 4 UIImage variables. I load all these 4 images into the 4 UIImageView on my storyboard.
-> Then I dismiss imagepicker
imagePicker.dismissViewControllerAnimated(true, completion: nil)
Here is the issue I am seeing in the end:
-> Sometimes, Image1 is same as Image 2, and sometimes all Images are the same. This happens randomly. I know for sure that all these 4 images should be unique. Because I load different html code in each step.
What Am I doing wrong in the sequence above?
A couple things you might try:
1) Don't reload webView in webViewDidFinishLoad. Instead wait until the next run loop on main thread (allowing iOS to fully finish). In objective C, my code would look like
dispatch_async (dispatch_get_main_queue(), ^{
// call method to load new html
});
2) I've had issues with webView not being refreshed by the time webViewDidFinishLoad was called. I solved this by adjusting the webView frame. Goofy, yes. This was back in iOS 6, so I have no idea if it makes any difference anymore or would affect what you are doing.
[webView loadHTMLString: html baseURL: nil];
// Play with frame to fix refresh problem
CGRect frame = webView.frame;
webView.frame = CGRectZero;
webView.frame = frame;
Is it possible to perform a screenshot of the current visible zone of the webview in Safari from a Share Extension? I could use windows, but UIApplication isn't supported on extensions so I can't access to that window.
You can't since UIApplication can't be reached from an extension. You cannot get the first UIWindow, which is the Safari layer, so you have to play with the Javascript preprocessing file that the extensions have. So just create a Javascript file that, when sent to Safari, generates a base64 string with the current visible zone image data. Take that string through the kUTTypePropertyList identifier in your extension. Since that should be NSData, generate the UIImage from there, by using +imageWithData. That is what you're looking for, without having to load the page again, preventing a second load and a bad image if the webpage requires of a login.
As far as I know, you can't unless you invoke the API you need dynamically, and even so you might run into context permission issues and app store approval issues.
An alternative might be passing the current Safari URL to your extension, load it using a hidden UIWebView and render this view into an UIImage but you will loose the current visible zone information...
Edit: So the below works in the Simulator but does not work on the device. I'm presently looking for a solution as well.
You can't get just the visible area of Safari, but you can get a screenshot with a little ingenuity. The following method captures a screenshot from a ShareViewController.
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
}
This is the approved way to capture the screenshot of a webpage in a share extension:
for (NSExtensionItem *item in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in item.attachments) {
[itemProvider loadPreviewImageWithOptions:#{NSItemProviderPreferredImageSizeKey: [NSValue valueWithCGSize:CGSizeMake(60.0f, 60.0f)]} completionHandler:^(UIImage * item, NSError * _Null_unspecified error) {
// Set the size to that desired, however,
// Note that the image 'item' returns will not necessarily by the size that you requested, so code should handle that case.
// Use the UIImage however you wish here.
}];
}
}
I have managed (with the help of this post) to open up a PLStaticWallpaperImageViewController from the PhotoLibrary private framework, which allows the direct setting of the wallpaper and lock screen (using same UI as the Photos app). Unfortunately, the image cropping/zooming features don't seem to work, as touches to the image view itself don't seem to be coming through (the main view is also not dismissed properly after the cancel/set buttons are touched, but this isn't so important).
I have an Xcode project demonstrating the wallpaper setting (can be run in simulator as well as a non-jailbroken device):
https://github.com/newenglander/WallpaperTest/
The code is quite basic, and involves a ViewController inheriting from PLStaticWallpaperImageViewController and implementing an init method similar to the following:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [self initWithUIImage:[UIImage imageWithContentsOfFile:#"/System/Library/WidgetResources /ibutton/white_i#2x.png"]];
self.allowsEditing = YES;
self.saveWallpaperData = YES;
return self;
}
(It will be necessary to allow access to the photo library after the first launch, and for some reason the popup for this comes up behind the app, rather than on top.)
Perhaps someone has insight as to why the cropping/zooming isn't working, or can give me an alternative way to set the wallpaper in an app (destined for Cydia rather than the App Store of course)?
Use this sample project, working very well.
Have inside camera control and custom layout, crop image when taken or after chose from your library, i used for my project and in very simple to customize.
https://github.com/yuvirajsinh/YCameraView
//---------- Answer improved----------//
I take a look on your project and i see 2 problem:
here you have 3 warning of semantic issue:
- (id)initWithUIImage:(id)arg1 cropRect:(struct CGRect { struct CGPoint { float x_1_1_1; float x_1_1_2; } x1; struct CGSize { float x_2_1_1; float x_2_1_2; } x2; })arg2;
in your ViewController.m you setting to get the image from where?
- (id)initWithCoder:(NSCoder *)aDecoder
{
// black_i
//what directory is this?
self = [self initWithUIImage:[UIImage imageWithContentsOfFile:#"/System/Library/WidgetResources/ibutton/white_i#2x.png"]];
//--------------------
self.allowsEditing = YES;
self.saveWallpaperData = YES;
return self;
}
i try to remove your
- (id)initWithUIImage:(id)arg1 cropRect:(struct CGRect { struct CGPoint { float x_1_1_1; float x_1_1_2; } x1; struct CGSize { float x_2_1_1; float x_2_1_2; } x2; })arg2;
change IMG directory in to:
self = [self initWithUIImage:[UIImage imageNamed:#"myImage.png"]];
and all working well but can't crop image, with my git hub YCameraView you have first understand how it work CROPPING function if you want to use crop or more simple, you have to create a fullScreen UICameraPicker allow user to get from camera or from library and allow the editing in cameraPicker then you can load a new picture in your View like this
self = [self initWithUIImage:[UIImage imageNamed:imageSelected.image]];
for a dismiss view, you can't because is a full app allow user to setUp background wallpaper and you can't terminate the app to see a SpringBoard, you have to create first view > picker > detail view with settings for a Home and LockScreen > then dismiss and come back to a first view.
PS: I think in your project to enable editing direct in a view you have to improve your code with a pinch and pan gesture on the UIView
Hope this help you!