ARKit2 Marker Images and Assets On-Demand - arkit

I have built an app that works really well if all resources and assets are within the app for 2D Markers.
But now, I'd like to pull marker 2d images on-demand from server, then use the downloaded 2D images to play content related to specific images.
I am having a hard time finding a solution for this. Any ideas or feedback will be greatly appreciated.
Thank you.

You can use Alamofire.
An example from their repo:
let downloader = ImageDownloader()
let urlRequest = URLRequest(url: URL(string: "https://httpbin.org/image/jpeg")!)
downloader.download(urlRequest) { response in
print(response.request)
print(response.response)
debugPrint(response.result)
if let image = response.result.value {
print(image)
}
}

Related

Rich notification play video clip

I'm trying to get rich notification and play video on the notification.
I success to show an image and not found swift sample code.
What need to be done to add video clip / mp4 notification support?
which function need to add to the NotificationService class ?
Thanks Yakir
There is no need to add extra code and no need to use AV player also. we can achieve that by initialising UNNotificationAttachment with url which we get after URLSession.shared.downloadTask
URLSession.shared.downloadTask(with: attachmentURL) { url, _ , _ in
let attachment = try UNNotificationAttachment(identifier: "identifier", url: url, options: nil)
content.attachments.append(attachment)
}
While updating the content in NotificationServiceExtension. Notification will load the video if it is video url.
Note: - please make sure video should be small size or as suggested by Apple.

How can I access SceneKit files downloaded from Firebase?

I have an ARKit app that uses image recognition to trigger SceneKit/3D Object files, specifically for art exhibitions.
I've recently began implementing Firebase Storage in order to reduce the overall download size and rather download the 3D files on demand, per which exhibition the user is using!
I've successfully set up the app to download the SceneKit files to the mobile storage, but where I am stuck now is figuring out how to read the file from the specific downloaded location and continue working it in the image recognition/AR process accordingly.
Before, when the Scenekit files were included in the initial download via Scenekit Catalog in the app folder, I would read them like so:
let ShipScene = SCNScene(named: "art.scnassets/ship.scn")
Now, I've updated it to read the same string as the download URL, but the image recognition is not working.
I assume my problem is that it is not reading the right location, or I may be using the wrong function. I've put the app through to my phone through TestFlight and still no luck.
// Downloading from firebase to device URL
let shipURL = documentsURL.appendingPathComponent("file:///var/mobile/Containers/Data/Application/QZ_Gallery/SceneKitFiles/ship.scn", isDirectory: true)
let shipDownload = shipRef.write(toFile: shipURL)
// Attempting to pull the file from the downloaded location
let ShipScene = SCNScene(named:"file:///var/mobile/Containers/Data/Application/QZ_Gallery/SceneKitFiles/ship.scn" )
*** My question is in the final line of code I included. I am trying to make sure I am searching in the right place to retrieve the files that were downloaded, or if the function I am using is even proper for retrieving files stored locally on a mobile device.
Solved by first adding a function to find the scenepath of the container which has all my scenes, and then using the scene path in the fetching of the individual .scn file. Scene downloads from Firebase Storage (No use of Realtime Database) and integrates properly with the image recognition.
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let scenePath = documentsURL.appendingPathComponent("Scenes")
if(!FileManager.default.fileExists(atPath: scenePath.absoluteString)) {
do {
try FileManager.default.createDirectory(atPath: scenePath.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error.localizedDescription);
}
}
// Start Download, writing to a file
let sceneURL = scenePath.appendingPathComponent("Ship.scn", isDirectory: true)

Placeholder image as Thumbnail(blurred) until actual image loads

I have below code for showing full screen image once User selects it.
fullImage.kf.setImage(with: url)
During the time the actual image downloads, I wanted to show a place holder as the same image but thumbnail size stretched out. This thumbnail was cached by Kingfisher library while showing thumbnails previously.
When I show the image with out KF library, by using regular iOS UIImage downloader it shows a blurred preview of thumbnail stretched and then it shows actual image. How can this be achieved with KF library?
you can use Place holder image like below
fullImage.kf.setImage(with: url, placeholder: UIImage(named: "placeholder"), options: nil, progressBlock: nil, completionHandler: nil)
But to show thumbnail you must have thumbnail image downloaded separately.
If you want the thumbnail to be an image form the web, you can do what people are suggesting on GitHub.
let cacheImage = ImageCache.default.retrieveImageInDiskCache(forKey: "cache")
let resource = ImageResource(downloadURL: imageURL, cacheKey: "cache")
imageView.kf.setImage(with: resource, placeholder: cacheImage, options: [.keepCurrentImageWhileLoading], progressBlock: nil, completionHandler: nil)
You'll need to find a way to download the thumbnails first though, which can be done in multiple ways, e.g. using an ImagePrefetcher

Load PDF in uiwebview and save to iBooks

I am creating an simple app which load pdf file from a particular website into UIWEBVIEW and i want to save it into iBook. I was finding many tutorial but doesn't make sense.I want to know how i am able to save pdf file which i have loaded in UIWEBVIEW and save it to iBook. Thank you.
let urlPDF = NSURL(string: "https://www.tutorialspoint.com/swift/swift_tutorial.pdf")
Here is code that i loaded it to uiwebview.
let request = NSURLRequest(url: urlPDF! as URL)
webView.loadRequest(request as URLRequest)

ios9 cache for NSData?

I use the following code to display UIImage loaded from Internet:
let imgURLString = "http://.../images/" + (self.userImage as String)
let imgURL = NSURL(string: imgURLString)
let imageData = NSData(contentsOfURL: imgURL!)
self.profilePictureImage.image = UIImage(data: imageData!)
That works good with the iphone5 simulator if the image is not too big.
But when I test it with my phone (ios9 iphone5), if I change the image on the server, the old image is still displayed.
I think there is a cache.
Is there a way to clear the cache? Or maybe a better approach to display images stored on a remote server?
Thank you for your feedback.
Regards,
Thomas
EDIT
I just found one interesting thing.
When I test in local network (through WIFI), it works fine with both simulator and iPhone.
But When I test remotely (through 4G), the image displayed is the old image. I have to connect the server with a web browser and refresh the images folder to make the new image display on the app.
Any idea to solve this?
I finally fixed it by changing the name of the new image stored on the remote server. Thus, I avoid any caching issue.