Creating webpages thumbnails in swift - ios

I am trying to code a "webpage selector" on my app, using a fancy carousel to display the webpages thumbnails.
However, I don't want to display the webpages thumbnails as webViews on my carousel because I can get quite a large number (20-30) of webpages at the same time and loading all of those can cause a lot of lag.
So, I decided to use screenshots of the webpages and use those images to populate my carousel instead, making the carousel animation much smoother.
Here is my question:
I would like to know if there is any way to take a screenshot of the webpage (without showing it on the app screen) and save the image into a list that I can use to populate the carousel.
I am aware of this question but is quite an old post, so I was wondering if there is another way to do it. Also, I tried to implement the suggested solution, but it did not work.

You can create a WKWebView that is behind your view controller (and thus invisible) and constrained to the size that you want. Once the page finishes loading take a snapshot by using drawHierarchy(in:afterScreenUpdates:) inside of a UIGraphicsImageRender and save the resulting image as your thumbnail.

Related

Displaying both video and image in a single view element

I'm attempting to create an Instagram styled app and cannot figure out a way to display media in a UITableView the way they do (either video or image in a single view). I know it most likely is a custom implementation of UIWebView or something of the sort, however I have no idea where to even begin writing a subclass like that. Things I've already tried include:
Using a UIWebView (massive memory leak, even when adding/removing the views programmatically).
Adding either UIImageView or UIWebView depending on whether or not it was a video or image (again had a memory leak)
Searching the internet for a possible way to do this
If anyone could help me out that would be awesome! FYI the media I am using is grabbed straight from my server, there is no data being gathered from a popular source such as YouTube, Flickr, etc.
i think they use UICollectionview as it allows for better customization of a table.
In terms of images and videos, use UIImageView to display all your images and MPMoviePlayerController to play videos.

Using PageViewController for a large number of pages like camera roll image view?

I am trying to create an app for which I need a way to swipe through views. For example, while swiping through images in photo stream or camera roll. I DO NOT need to swipe through images however, I need to swipe through UIViews. I have been looking at PageViewController. However, setViewControllers:direction:animated:completion: suggests that I need a ViewController for every UIView. This doesn't seem to be what I want since I will have over 40 views that I will need to scroll through. I think I'm missing an obvious ios feature here since even iBooks has a way to go through pages of books.
I was thinking maybe having 3 ViewControllers and changing their content whenever I swipe across. But that seems very roundabout.
Please help me out!
Thanks!
Apple has provided a fairly robust example of doing this kind of thing with photos called "PhotoScroller" at http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html. It uses UIScrollViews and CATiledLayers to recreate the experience of the Photos app. You may be able to modify it to work with your UIViews instead of images.

UIScrollView: Bad performance with large images

TL:DR
What technique does Apple use to make Photo.app so fast, even with large images?
Long Version
I watched Apple's WWDC 2010 video about scroll views to learn how to replicate Photo.app pagination behavior and low memory utilization (PhotoScroller Demo). It works well, but since images are loaded only when they are needed, when I try to paginate to another image, the app locks while the JPEG is being decompressed.
The same video shows a tiling technique to get better performance, but since I'm using photos taken from the camera and stored in the app, that doesn't seem feasible (having multiple copies of each photo, in different resolutions, would consume too much space - 4MB vs 27MB). Also, using iExplorer I noticed Photo.apps has only a copy of each photo (it doesn't even have a small thumbnail copy for the gallery).
What technique did Apple use to make Photos.app so fast? How can I get that same performance in my app?
I'm a bit confused if this should be here or on Programmers,
since there's no code in the question, but F.A.Q. says that algorithm
questions are part of Stackoverflow, and the tags here match it
better.
So if you just show one image fullscreen you can do this:
In the WWDC11 Session 104 - Advanced Scroll View Techniques they talk about infinite scrolling and how to do it. The basic idea is to scroll the view and after scrolling reposition the (UIImage)view inside the scroll view so it appears centered or whatever you layout constraints are.You could then load the new UIImage into the UIImageView. Since you only have one UIImageView it should be pretty low memory consuming. I am not sure about how the loading times of the images will behave though.
Maybe preload the next UIImage to the left and right to the current image and then load it into the UIImageView after reposition the scrollView can help here.
For anyone who is still looking for simply implementation of scroll view that hold lot's of images.
https://github.com/sumofighter666/ReusableScrollView
It is as simply as implementing UITableView

ios - how to create a screen with a lot of text and buttons and not use UIWebView

I can not use UIWebView because I want the text to be accessible even if the user does not have Internet access on their phone. Also, I will need to have buttons to other screens on the app and I think that is not possible with UIWebView (right?)
Is there a way for me to edit the xml of the screen directly inside Xcode? My requirement is to have about 10 sections with header labels and buttons before each, so if I have to adjust it on the storyboard screen, it will be a bit nightmarish.
What is a reasonable approach for me here? I think only to edit the xml by hand and hardcode it with styles and text, right?
several things here:
webviews only need an internet connection if you are downloading the content for the view (which doesnt seem like what you want anyway, aren't you just building it locally?)
you can make the buttons in a webview do whatever you want. See this answer.
What is so complex about this UI that you can't just build it the regular way in code or IB?
Just because you consider using a UIWebView it doesn't mean you are restricting your application functionality to internet access.
You can just as well have HTML documents in the application bundle that you display in a webview. This is basically what PhoneGap is doing.
If you don't want to use webviews then nothing is stopping you from using UIViews, UILabels, UIImageViews etc and compose your app how you want.

iPhone OS 3.2; PDF rendering; User Interaction

I'd need to create a iPad-app which would be rendering multiple PDF-Files (one file contains one page).
Each page should be scrollable, zoomable and if the user taps on a part of the PDF a website or photo gallery should popup.
Currently i think i could do that either with:
A. UIWebView
Displays the pdf's nicely, scrolling and zooming works. But it looks like a lot of trouble to realize the clickable parts of the PDF.
I don't know if i could use CGPDFContextSetURLForRect
Getting the touch-events from UIWebView to do something like CGPDFContextSetURLForRect my self looks like it would be some "quite bad" hack. See: http://github.com/psychs/iphone-samples/blob/master/WebViewTappingHack/Classes/PSWebView.m
B. Quartz
I found some resources describing how to display PDF's directly via Quartz.
See:http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html
This would allow using CGPDFContextSetURLForRect
But i have no idea if this would - like UIWebView - support scrolling and zooming out of the box?
Anybody could enlighten me on this please?
Thanks for your time!
[Edit: changed 3.0 to 3.2]
[Edit: my "solution"]
Hi!
I could come up with a working implementation for PNG but not for PDF's.
[Abstract]
My sollution was Rendering the content, intercepting the touches on it, retreiving the coordinates relative to the displayed content if it is one touch and finally looking up what to do from a mapping containing the interactive areas as coordinates and what to do if they get clicked.
[For PNG]
It was way more cumbersome to implement somethink like that than i would have imagined...
And the implementation i got working depends heavily on the content you want to display because this does work for UIImageView but i could not get it working with UIWebView.
First you need a UIScrollView and UIImageView to render the content and support scrolling/zooming.
Then you need to implement some handling to get the touches/gestures you are interested in.
See: developer.apple.com/iphone/library/samplecode/ScrollViewSuite/Listings/1_TapToZoom_Classes_TapDetectingImageView_h.html
This sample from apple provides everything you need to get this part working.
As a bonus it also takes care about transforming the coordinates relative to the viewport of the content which is very handy! (else you would only know where the tap happend on the screen wich only one half of the info you need if your content is zoom-/scrollable)
[For PDF]
If you want to do this with PDF the first thing would be that you need to use a UIWebView (probably you could do it via Quartz or something else too)
Getting the touches with a UIWebView is a real pain!
There are a lot of ways proposed on the web and besides one noone did what it should do.
After days of googling i found this gem: cocoawithlove.com/2009/05/intercepting-status-bar-touches-on.html
So... subclassing UIWebView does not get you anywhere unlike UIImageView and you have to subclass UIApplicationMain and implement its method for handling touch-events.
Here you could reuse some of the "Touch-Handling-Stuff" from the apple-examlpe from above.
Now you would need to translate the coordinates of the touch to your content if it is zoom-/scrollable. UIWebView DOES NOT do this for you unlike UIImageView!
I could never figure out how to get the required information(what part of the content at which zoomlevel) from a UIWebView to translate the coordinates but due to the changed requirements from PDF to PNG i didn't care to get it working too much.
hope this helps.
Using the CGPDF* operators will allow you to write a UIPDFView, which operates exactly as UIImageView but uses a PDF as the source image. Create your own custom subclass of UIView and implement drawRect: to, eventually, call CGContextDrawPDFPage. Based on a quick Google search (because I know the keywords), this page seems to explain that side of things quite well.
You can then directly substitute your custom UIView subclass for the UIImageView and proceed exactly as you have with the PNG solution.

Resources