Skip to content

Commit

Permalink
Improve ios image loader
Browse files Browse the repository at this point in the history
  • Loading branch information
cl3m committed Jan 19, 2021
1 parent 9f41980 commit 3b301d0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
18 changes: 18 additions & 0 deletions iosApp/iosApp/HostingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import test
import Network

class HostingControllerProxy: UIViewController {
var hostingController: HostingController!
Expand All @@ -25,6 +26,8 @@ class HostingControllerProxy: UIViewController {
let resources = Resources(logo: "logo")
hostingController = HostingController(controller: self, content: {
ContentKt.Content(resources: resources)
}, imageViewLoader: { imageView, url in
imageView.load(url)
})
hostingController.viewDidLoad()
}
Expand Down Expand Up @@ -60,3 +63,18 @@ class HostingControllerProxy: UIViewController {
hostingController.refreshContent()
}
}

extension UIImageView {
func load(_ url: String) {
guard let url = URL(string: url) else { return }
DispatchQueue.global().async { [weak self] in
if let data = try? Data(contentsOf: url) {
if let image = UIImage(data: data) {
DispatchQueue.main.async {
self?.image = image
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlin.math.max

val DEBUG_COMPOSE = true

open class HostingController(val controller: UIViewController, val content: @Composable () -> Unit) {
open class HostingController(val controller: UIViewController, val content: @Composable () -> Unit, val imageViewLoader: (UIImageView, String) -> Unit) {
@ThreadLocal
companion object {
lateinit var host: HostingController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,18 @@ actual inline fun Image(url: String,
}

@ExportObjCClass
class ComposeImageView(bitmap: ImageBitmap?, val url: String?) : UIImageView(frame = cValue { CGRectZero }), NSURLSessionDelegateProtocol, NSURLSessionDataDelegateProtocol {
class ComposeImageView(bitmap: ImageBitmap?, val url: String?) : UIImageView(frame = cValue { CGRectZero }) {
var isDirty: Boolean = false
var contentIdentifier: String
var task: NSURLSessionDataTask? = null
var session: NSURLSession? = null
var imageData = NSMutableData()

init {
contentIdentifier = contentIdentifier(bitmap, url)
if (DEBUG_COMPOSE) NSLog("🔴 [init ComposeImageView] $contentIdentifier")
bitmap?.let { image = (it as iosImageBitmap).toUIImage() }
url?.let { downloadImage(it) }
url?.let { HostingController.host.imageViewLoader(this, url) }
contentMode = UIViewContentMode.UIViewContentModeScaleAspectFit
}

private fun downloadImage(urlString: String) {
val url = NSURL(string = urlString)
if (url != null) {
session = NSURLSession.sessionWithConfiguration(NSURLSessionConfiguration.defaultSessionConfiguration(), delegate = null, delegateQueue = NSOperationQueue.mainQueue())
task = session!!.dataTaskWithURL(url) { data, _ , _ ->
if (DEBUG_COMPOSE) NSLog("🔵 [dataTaskWithURL ComposeImageView]")
if (data != null) {
image = UIImage(data = data)
}
}
task!!.resume()
}
}

companion object {
fun createOrReuse(bitmap: ImageBitmap? = null, url: String? = null): ComposeImageView {
for (view in getCurrentView().subviews) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.rouge41.kmm.compose.test

import com.rouge41.kmm.compose.Composable
import com.rouge41.kmm.compose.Text
import platform.UIKit.UIImageView
import platform.UIKit.UIViewController
import com.rouge41.kmm.compose.HostingController as HC

class HostingController(
controller: UIViewController,
content: @Composable() () -> Unit
) : HC(controller, content)
content: @Composable() () -> Unit,
imageViewLoader: (UIImageView, String) -> Unit
) : HC(controller, content, imageViewLoader)

@Composable
actual fun Test() {
Expand Down

0 comments on commit 3b301d0

Please sign in to comment.