Load url into iOSImage

Is there a way to load an url link with image directly into a iOSImage?

Perhaps you could use an HTTPSocket to get the image to a file and then create the iOSImage from the file?

Yes, that would be a way to do it. Not sure if this is the best/fastest way to do this.

Maybe Xojo Inc could add a new iOSImage method .loadFromUrl ?
This is something that would be very useable because a lot of online APIs (Weather, Twitter, Facebook, …) uses image urls to download images.

Try this (untested). Once we have threads for iOS you should call it from a thread since it is synchronous.

Function LoadImageFromURL(url as Text) as iOSImage const FoundationLib = "Foundation.framework" const UIKitLib = "UIKit.framework" declare function NSClassFromString lib FoundationLib (clsName as cfstringref) as ptr declare function URLWithString lib FoundationLib selector "URLWithString:" (clsRef as ptr, str as cfstringref) as ptr declare function dataWithContentsOfURL lib FoundationLib selector "dataWithContentsOfURL:" (clsRef as ptr, url as ptr) as ptr declare function imageWithData lib UIKitLib selector "imageWithData:" (clsRef as ptr, data as ptr) as ptr dim urlRef as ptr = URLWithString(NSCLassFromString("NSURL"),url) dim dataRef as ptr = dataWithContentsOfURL(NSClassFromString("NSData"),urlRef) //synchronous part - maybe display a spinning progress wheel? dim uiimageRef as ptr = imageWithData(NSCLassFromString("UIImage"),dataRef) return iOSImage.fromHandle(uiimageRef) end function

[quote=158218:@Jason King]dim dataRef as ptr = dataWithContentsOfURL(NSClassFromString(“NSData”),urlRef) //synchronous part - maybe display a spinning progress wheel?
[/quote]

If you do this, your application will hang and potentially be killed by the OS if it waits long enough.

Is there another option? We don’t have a way to move it to a background thread, right?

[quote=158218:@Jason King]Try this (untested). Once we have threads for iOS you should call it from a thread since it is synchronous.
[/quote]

Thank you Jason. It works great :slight_smile:

For small pictures, it seems to be fine over WiFi. It could be an issue over cellular data network, though. They could be much, much slower. The LR mentions sending is asynchronous, but does not say for receiving. I gather it is, right ? Since storage is done in memory, using a temporary file should be almost as fast as LoadImageFromURL.

HTTPSocket?

Even synchronous DNS lookup has a longer timeout than the iOS application killer. This will cause crashes on some subset of your user base unless they’re all on wifi or very reliable cell service, even if it appears to work in your testing.

I’m not sure what you mean about the LR, since it’s not a function call in the framework.

[quote=158248:@Joe Ranieri]@Michel Bujardet The LR mentions sending is asynchronous, but does not say for receiving. I gather it is, right ? Since storage is done in memory, using a temporary file should be almost as fast as LoadImageFromURL.
I’m not sure what you mean about the LR, since it’s not a function call in the framework.[/quote]

Sorry, I did not express myself right. I think HTTPSocket and a temporary file is much better, since the device is using memory for file storage, which should be almost as fast as imagefromurl. My question had to do with HTTPSocket, which the LR says is asynchronous for sending, but does not say for receiving. I assume it is.

HTTPSocket is entirely asynchronous on iOS.

Great. Thanks.