gpt4 book ai didi

ios - 在 swift 中通过 inout 参数写一个 var

转载 作者:行者123 更新时间:2023-11-28 09:15:36 25 4
gpt4 key购买 nike

我想将多张图片加载到一个模型中。我试图用一个 loadImage() 函数和一个 inout 参数来解决这个问题。但由于某种原因,图像变量总是空的。我没有看到图像。

这里有什么问题吗?

   public var image: UIImage = UIImage()

// Somewhere in the init function
self.loadImage("http://www.domain.com/cats.img", targetImage: &self.image)

func loadImage(url:String, inout targetImage:UIImage) {
dispatch_group_enter(self.dispatch_group);

println("Start loading image \(url)")

var request:Alamofire.Request = Alamofire.request(.GET, url).responseImage() {
(request, _, image, error) in
if error == nil && image != nil {
println("imageRequestSuccess")

// Save the image to the model property
targetImage = image!

// Dispatch if success
dispatch_group_leave(self.dispatch_group)
} else {
println("imageRequestFailure")

// Dispatch also to handle failure
dispatch_group_leave(self.dispatch_group)
}
}
}

最佳答案

使用闭包效果很好......但看起来很丑

self.loadImage(self.image_url!, onSuccess: { s1 in return self.image = s1 })



func loadImage(url:String, onSuccess: (s1:UIImage) -> () ) {
dispatch_group_enter(self.dispatch_group);

println("Start loading image \(url)")

var request:Alamofire.Request = Alamofire.request(.GET, url).responseImage() {
(request, _, image, error) in
if error == nil && image != nil {
println("imageRequestSuccess")

onSuccess(s1:image!)

// Dispatch if success
dispatch_group_leave(self.dispatch_group)
} else {
println("imageRequestFailure")

// Dispatch also to handle failure
dispatch_group_leave(self.dispatch_group)
}
}
}

关于ios - 在 swift 中通过 inout 参数写一个 var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27777024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com