我有这个图像渲染功能,导致主/ UI线程在渲染时阻塞/断断续续。
有什么方法可以阻止线程并在iOS中的其他线程上呈现?是否有原生API可以帮助您?
更新了代码:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
function
{
UIImage *shrinkedImage = [ThisClass imageWithImage:screenShotImage scaledToSize:shrinkImageToSize];
UIImage * rotatedImage = [[UIImage alloc] initWithCGImage: shrinkedImage.CGImage
scale: 1.0
orientation: UIImageOrientationRight];
}
由于
答案 0 :(得分:3)
你可以考虑一些东西:
time profiler
的{{1}}检查大部分时间的内容。 instruments tool
代替UIImageView
来应用某些转换,例如轮换和转换。您需要将这些转换应用于其UIImageView的CALayer。UIImage
将图片加载到后台主题中。这是一个例子:GCD
答案 1 :(得分:2)
您是否尝试使用GCD在后台执行任务?
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// Do your computations in the global queue (background thread)
UIImage *shrinkedImage = [ThisClass imageWithImage:screenShotImage scaledToSize:shrinkImageToSize];
UIImage * rotatedImage = [[UIImage alloc] initWithCGImage: shrinkedImage.CGImage
scale: 1.0
orientation: UIImageOrientationRight];
// Once done, always do all your display operations to update the UI on the main thread
dispatch_sync(dispatch_get_main_queue(), ^{
yourImageView.image = rotatedImage;
});
});
有关更多信息,请阅读Apple的Concurrency Programming Guide。