How can I display images in a carousel view from web? - ios

I have an an xml file in the web, which contain few image urls (number of urls are changing dynamically) So I want to show that images in a carousel view.
What I'm going to do is, read the xml file and get each image url. Then download the image in to the phone and add them in to a carousel view.
Is there any easy or stranded way to do that ?

Since you're using the keyword "carousel", it sounds like you already know about the many open source implementations of Coverflow-like image display.
The trick for your app is that you need to create a data source (e.g. a NSMutableArray) while you are parsing your XML and the iCarousel (or whatever you decide to use) view will draw from that array to display the images.
If this were my app, I'd store custom objects in the data source array which would contain the image URL and, when the object is created, would dispatch an asynchronous thread to download the image (which would then also get stored into that custom object).
Your question was rather broad, and I'm not able to give a super detailed solution since actual code might not be applicable for how you decide to architect your yet-to-be-coded app.

Related

Creating webpages thumbnails in swift

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.

Fill the View Controller with elements/objects from web/cloud swift

I have a TableViewController which is connected to a empty-"template" viewController. I would like to make each cell responsible for its own interface/design.
In other words: the viewController has all elements placed (like UIImage, UILabel, UIText, etc.) and each time when specific cell is selected the viewController starts to fill with specific images/resources. Moreover, it would be great if the resources will be taken from web or cloud (in order to not save everything in the application itself).
So, I imagine it somehow like this:
Flow sketch
The problem is that I deal with this for the first time, and tried to find different ways to solve this problem (in terms of implementation), therefore I would like to ask: does this idea can be implemented in this way or probably there is more reliable way, and which techniques or technology can be used for this realization?
Thank you!
You can do it in multiple approaches. It totally depends on the architecture you have been following.
You can use react/ key-value observing and set the keys when you get a response.
Alternatively, for a very small app or a POC, I would use alamofire as my network manager and as soon as I get a response, I would set the labels. I would use the image extension in alamofire to set the images as it takes more time to download.
I would want to know the exact problem you are facing to help you out. Generic discussion are not normally encouraged here.

Is it possible to take an image from a UIWebView and display it natively without reloading?

Say I load a webpage with an image on it. Is there a way to reuse this loaded image within a UIImageView without reloading the image from a url?
If the image is cached by URL loading system, it will come from the cache when you try to load it.
Sorry I can't give a large explanation as im on y phone but I feel like what your looking for is 'core data store' there are several examples of how to use this framework, but it's quite complicated and has a large learning curve with ios development.
Also you say 'a' uiwebview. Does that mean that the URL your accessing to show within your uiwebview is static? If so, just copy and save the image and store the image in your project for future reference.
Or are you allowing the user to keep swapping through links in the uiwebview and store all those images that popup on the web page?
Sorry I'm just unclear with the question... Please no down votes :)

Download an image from the web and caching it by using Core Data

Imagine you use a web service that delivers images via an API. Every image has an UID and you have a Core Data entity Image with attributes uid (int) and image (transformable).
Now a gallery of your app needs to show many images (the UIDs are known). You don't know which of the images have been downloaded and stored before. How can you lazily download images with core data in the background, so that the view may show an UIActivityIndicator during loading and automatically shows the image as soon as it is stored locally (e.g. by using the NSFetchedResultsControllerDelegate protocol)?
Is it useful to subclass UIImageView for that purpose?
Yes, you can use Core Data for this, but be forewarned that there is a performance hit when you use Core Data to store images. It's negligible if you're dealing with small (e.g. thumbnail) images, but for very large images, the performance hit is observable. For large images, I'd store the images in Documents folder (or, better, a subfolder), and use Core Data to keep track of what images have been downloaded, their filenames, etc. But if the images are smaller, keeping everything right there in Core Data is cleaner.
I probably would not want to use a subclassed UIImageView for this purpose, because you might want to decouple the presentation layer (the image view) from the caching of images. Also, for sophisticated user interfaces, UIImageView objects may be discarded or reused as the user scrolls through a big collection of images, so you might not want a hard link between the UIImageView and your caching logic. Also, depending upon your user interface, sometimes the images can dictate something broader than the UIImageView (e.g., if you're using a tableview, you might want to adjust the cell height based upon the image as it's downloaded). The particulars of the implementation of what the UI might do depend upon where the image is being used (a UITableView, a UIScrollView that is showing a grid of images, etc.).
So, bottom line, for simple user interfaces, perhaps you could subclass a UIImageView but I'd generally advise to have some custom image caching object that does the lazy loading with some delegate protocol to tell the UI when the image load is complete.
I wouldn't use Core Data to store images, but I'd store them on disk. I was trying to find in the documentation where Apple themselves warn against storing images larger than 100k or so in Core Data since you'd run into a performance issue.
However, I found this article that talks about Core Data Image Caching that may be of use.
Also, here's another Stack Overflow post with a good answer that tells you when to store images in a database and when to just use references to disk storage.
If you don't absolutely need Core Data, then may I recommend using MWPhotoBrowser?
https://github.com/mwaterfall/MWPhotoBrowser
It essential generates a gallery view controller for you which you can push onto your navigation controller. The gallery view controller has scrollable images with pinch zoom, pan, everything, even emailing the photo to someone.
It also does the lazy loading of the image with the activity indicator.
Short answer: everything you wanted to do without reinventing the wheel.

How do I make a Deep Copy of a UIElement?

So I have a printing component that serves a Silverlight application. Other modules in this program have the ability to signal the printing component and pass it a UIElement, which the printing component will then draw to the screen. All well and good. The problem arises when I try to manipulate the UI Element in order to better format it to fit the user's selected paper size or anything to that effect; it seems that the UI element that is passed in is frequently the exact same instance of the one on the screen, and the screen element changes itself to match the 'print-only' changes I have made. For now, I can manually save the previous values, make my changes, and restore the previous values, but it would be easier/more robust/more efficient/more flexible if I had a way to, given the UI element, make a copy of the element, and manipulate that freely, without worrying about alterations or state on the original UI element. How can I programatically copy an instance of a UI element such that I have another instance with the same visual appearance?
I know 2 ways you can try:
Save the object to a xaml string and recreate it from it.
(XamlWriter.Save and XamlReader.Parse)
Save the object with the serializer to a memorystream and recreate it from that - it is possible that not all objects are marked serializable so the other option might be the one to use.
It might seem a bit much - but there are not so many ways to create a deep copy - and not any standard c# method that I know of.

Resources