how to use MTKTextureLoader newTextureWithContentsOfURL to retrieve an image from the web? - ios

Is it possible to use MTKTextureLoader.newTextureWithContentsOfURL to retrieve an image from the web (ex: https://i.stack.imgur.com/EwPfY.jpg?s=48&g=1)? I try but I didn't succeed, so maybe I did something wrong

The -newTextureWithContentsOfURL:... methods are intended for use with file URLs, not Web URLS.
If you want to load an image from the Internet, you should use NSURLSession and NSURLDataTask to download the image contents first (asynchronously), and then use MTKTextureLoader's -newTextureWithData:options:error: method or its asynchronous counterpart to create a MTLTexture.

Related

can I provide URL to SDWebImageCache which returns image either from cache or download?

I am using SDWebImageCache I got a requirement where I need to provide an URL where it should automatically download the image asynchronously and if it is in cache it should get from cache.
I have observed we can set image to imageView with URL but did not found any method which asynchronously downloads and gives UIImage in return.
You can find a more extensive documentation for SDWebImage here.
For downloading an image directly without the use of a category you will need to use SDWebImageManager
For downloading the image and getting the same in a closure, use the loadImageWithURL:options:progress:completed: method.
For just caching the image you can use the saveImageToCache:forURL: method.

Downloading images directly from Parse.com, as opposed to using their API

I noticed that for a PFFile type Object stored on my Parse.com developer account, the link is open and accessible for anyone to view/download.
Example a PFFile Object that is named, name.jpg, representing an image could be a URL like :
http://files.parsetfss.com/<some garbled class UUID>/<some garbled image uuid>-name.jpg
Where <some garbled class UUID> appears to be the same for all name.jpg, stored on a class
and <some garbled image uuid>-name.jpg appears to be unique uuid's appended with the actual object name which is 'name.jpg'
Using the above URL, anyone/any client can download the object
So I have some questions regarding this:
Is this normal? Is this by design?
Will the URL for an object change, if nothing else changes?
Am I being unwise in using this information to download images directly, thereby saving the cost of one API call from Parse (although I think I'll make one API call anyways to get the URL) ?
Will downloading directly from this URL, perform better/acceptable compared to downloading via Parse.com API
Yes it's normal. Protection is via the object, don't give access to anyone who shouldn't have it.
No the URL shouldn't change, though strictly you should query from the file object each time you want it to be sure.
You should care more about network calls that API calls in general. You could use cloud code to aggregate responses, or batch requests, but that doesn't reduce API calls.
The download is unchanged as you're always downloading the same file from the same link no matter which API you use to do it.

Generate file-directory-safe string using URL?

My app is sent messages which sometimes include http links to images. When this happens, I want to cache the image locally, so I can display the image in future without needing to download it, if it exists locally.
But lets say the image url is http://imgur.com/abcdef.jpg. A file path of something like this, I'd imagine could have all sorts of issues:
/var/Applications/[application]/documents/http://imgur.com/abcdef.jpg
What I need is a way to take the given image URL and generate a file directory-friendly string, so that I can save the image using that URL every time, or check for its existence.
While there are many ways how to implement it (simpliest way would be to create MD5 of the web address, store image as [md5name.png] and then just try to check if md5 file reference exists), I suggest you use one of two libraries made for that:
https://github.com/Haneke/Haneke
https://github.com/rs/SDWebImage
They both work on url-based principles, provide you with all known implementations of caching and they handle cases like this by default. Both of them also download images in background which is recommended way to do it and have convenience methods to load images in UIImageView.
Hope it helps!

Asynchronous Multiple URL Pullers in iOS

I was trying to solve this challenge for fun, http://tleyden.github.io/blog/2014/01/13/objective-c-coding-interview-challenge-urlpuller/, where it asks to asynchronously pull content for an array of urls. Now, I know how to pull content for one url using asynchronous methods of NSURLConnection using initWithRequest, Does this mean I have to create an array of NSURLConnections and requests to pull this off?
yes. you have to create an array of NSURLConnection. But how about already-built network library like:
https://github.com/AFNetworking/AFNetworking
http://allseeing-i.com/asihttprequest/ (Discontinued, but very powerful)
you can also use Cocoapods to import it. that's my favourite way

Where do Instagram/Facebook store images internally?

Like the title says. In an iPhone, in what directory do Instagram/Facebook (and similar applications) store the images they downloaded at runtime?
When is that directory wiped?
For non permanent images (e.g. post thumbnails but not user profile pics) I would imagine they get stored in the <Application_Home>/Library/Caches, other content probably goes in Documents
You don't need to know what facebook or instagram do, but If I understand your question right, you want to download images asynchronously from web and cache them. For that u can use a third party class to handle the caching instead of writing your own, I would suggest SDWebImage.

Resources