I'm trying to create a UIImage with one of Apple's new layered image files that's hosted on a remote server.
我正在尝试用苹果的一个新的分层图像文件创建一个UIImage,它驻留在一个远程服务器上。
The sample code below downloads the lsr file correctly (the data
var holds a value), but creating a new NSImage with it results in a nil value. Ignore the fact that this code is synchronous and inefficient.
下面的示例代码正确地下载了lsr文件(数据var保存了一个值),但是使用它创建一个新的NSImage会得到一个nil值。忽略这段代码是同步和低效的这一事实。
if let url = NSURL(string: "http://path/to/my/layered/image.lsr") {
if let data = NSData(contentsOfURL: url) {
let image = UIImage(data: data) // `image` var is nil here
imageView?.image = image
}
}
Any thoughts on how to download an LSR and create a UIImage with it?
对于如何下载一个LSR并使用它创建一个UIImage有什么想法吗?
1 个解决方案
#1
2
That's how i solved it:
我就是这样解决的:
- Convert you .lsr file to a .lcr file doing this from console:
xcrun --sdk appletvos layerutil --c your_file.lsr
- 将.lsr文件转换为.lcr文件,从控制台:xcrun—sdk appletvos layerutil—c your_file.lsr执行此操作
- Upload your_file.lcr on your server
- 上传your_file。电感电容电阻测量服务器上
-
Put these two functions into an util class:
将这两个函数放到util类中:
func getDataFromUrl(url:NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError? ) -> Void)) { NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in completion(data: data, response: response, error: error) }.resume() } func downloadImage(url: NSURL, imageView: UIImageView){ print("Started downloading \"\(url.URLByDeletingPathExtension!.lastPathComponent!)\".") getDataFromUrl(url) { (data, response, error) in dispatch_async(dispatch_get_main_queue()) { () -> Void in guard let data = data where error == nil else { return } print("Finished downloading \"\(url.URLByDeletingPathExtension!.lastPathComponent!)\".") imageView.image = UIImage(data: data) } } }
-
Use it like this:
使用它是这样的:
if let checkedUrl = NSURL(string: "http://domain/path/to/your_file.lcr") { self.my_ui_view.contentMode = .ScaleAspectFit downloadImage(checkedUrl, imageView: self.my_ui_view.contentMode) }
This will use the image without saving it into the document directory, if you need that solution, ask me and i'll share.
这将使用映像而不会将其保存到文档目录中,如果您需要该解决方案,请询问我,我将与您共享。
#1
2
That's how i solved it:
我就是这样解决的:
- Convert you .lsr file to a .lcr file doing this from console:
xcrun --sdk appletvos layerutil --c your_file.lsr
- 将.lsr文件转换为.lcr文件,从控制台:xcrun—sdk appletvos layerutil—c your_file.lsr执行此操作
- Upload your_file.lcr on your server
- 上传your_file。电感电容电阻测量服务器上
-
Put these two functions into an util class:
将这两个函数放到util类中:
func getDataFromUrl(url:NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError? ) -> Void)) { NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in completion(data: data, response: response, error: error) }.resume() } func downloadImage(url: NSURL, imageView: UIImageView){ print("Started downloading \"\(url.URLByDeletingPathExtension!.lastPathComponent!)\".") getDataFromUrl(url) { (data, response, error) in dispatch_async(dispatch_get_main_queue()) { () -> Void in guard let data = data where error == nil else { return } print("Finished downloading \"\(url.URLByDeletingPathExtension!.lastPathComponent!)\".") imageView.image = UIImage(data: data) } } }
-
Use it like this:
使用它是这样的:
if let checkedUrl = NSURL(string: "http://domain/path/to/your_file.lcr") { self.my_ui_view.contentMode = .ScaleAspectFit downloadImage(checkedUrl, imageView: self.my_ui_view.contentMode) }
This will use the image without saving it into the document directory, if you need that solution, ask me and i'll share.
这将使用映像而不会将其保存到文档目录中,如果您需要该解决方案,请询问我,我将与您共享。