是否有使用dispatch_sync和dispatch_after混合的最佳方式?
我必须遍历一个Views列表,然后等待加载数据。然后进一步滚动并等待加载数据。等等。
除非此dispatch_sync已完成,否则我不想执行其余的代码行。
参考代码: 假设:
我已经确定了上下文的图像大小。
UIGraphicsBeginImageContextWithOptions ? UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0):UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Render the layer for the image.
for (UIView *view in aFinalListToRender) {
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *aView = (UIScrollView *)view;
for (int i = 0; i < ceil(aView.contentSize.height/aView.frame.size.height); i++) {
for (int j = 0; j <= ceil(aView.contentSize.width/aView.frame.size.width); j++) {
// Scroll smartly for retina and non-retina devices
[aView scrollRectToVisible:CGRectMake(aView.frame.size.width * j, aView.frame.size.height * i - (isRetina ? kRetinaValue: 0), aView.frame.size.width, aView.frame.size.height) animated:NO];
// TODO: Need to apply a synchronous-yet-asynchronous wait such that,
// the current execution of the method pauses here,
// yet allowing main thread to complete the execution, like rendering images etc,.
[aView.layer renderInContext:context];
}
}
} else {
[view.layer renderInContext:context];
}
}
aReturnVal = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();