gpt4 book ai didi

ios - 照片框架。 requestImageForAsset 返回两个结果。无法设置 ImageView

转载 作者:IT王子 更新时间:2023-10-29 08:01:53 25 4
gpt4 key购买 nike

所以我使用 SwipeView 库 ( https://github.com/nicklockwood/SwipeView ) 来显示使用 iOS8 Photos 框架的图像。

但是,当我调用 requestImageForAsset 时,我注意到我得到了两个结果,一个是缩略图大小,另一个是我想要的更大的大小。但是,较大的图像没有及时加载(我理解它称为异步)以返回,因此它返回小图像。

这段代码可能更有意义。

    func swipeView(swipeView: SwipeView!, viewForItemAtIndex index: Int, reusingView view: UIView!) -> UIView! {
let asset: PHAsset = self.photosAsset[index] as PHAsset

var imageView: UIImageView!

let screenSize: CGSize = UIScreen.mainScreen().bounds.size
let targetSize = CGSizeMake(screenSize.width, screenSize.height)


var options = PHImageRequestOptions()
// options.deliveryMode = PHImageRequestOptionsDeliveryMode.Opportunistic
options.resizeMode = PHImageRequestOptionsResizeMode.Exact


PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: targetSize, contentMode: .AspectFill, options: options, resultHandler: {(result, info)in
println("huhuhuh")
println(result.size)
println(info)
imageView = UIImageView(image: result)
})
println("setting view)
return imageView
}

这是日志输出:

Enteredhuhuhuh
(33.5,60.0)
SETTING VIEW
huhuhuh
(320.0,568.0)

如您所见,它会在接收到大图像之前返回 ImageView 。我如何让它返回这个更大的图像而不显示缩略图?

谢谢。

最佳答案

读取 PHImageManager 类的 header

If -[PHImageRequestOptions isSynchronous] returns NO (or options is nil), resultHandler may be called 1 or more times. Typically in this case, resultHandler will be called asynchronously on the main thread with the requested results. However, if deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic, resultHandler may be called synchronously on the calling thread if any image data is immediately available. If the image data returned in this first pass is of insufficient quality, resultHandler will be called again, asychronously on the main thread at a later time with the "correct" results. If the request is cancelled, resultHandler may not be called at all. If -[PHImageRequestOptions isSynchronous] returns YES, resultHandler will be called exactly once, synchronously and on the calling thread. Synchronous requests cannot be cancelled. resultHandler for asynchronous requests, always called on main thread

因此,您要做的是让 resultHandler同步调用

PHImageRequestOptions *option = [PHImageRequestOptions new];
option.synchronous = YES;

[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:target contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage *result, NSDictionary *info) {
//this block will be called synchronously
}];

所以你的 block 将在结束你的方法之前被调用

祝你好运!

关于ios - 照片框架。 requestImageForAsset 返回两个结果。无法设置 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27670325/

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