2012-09-23 28 views
1

我有这个图像渲染功能,这是导致主/用户界面线程阻止/口吃时,我渲染它。如何呈现图像而不阻挡用户界面?

什么方法可以阻止线程并在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

请发表你使用渲染图像的代码。 – mark

+0

用用于渲染的代码更新。 – mskw

+2

你为什么要调整大小?如果您只是想在屏幕上显示它,请使用图像视图并将其设置为缩放,在其上应用旋转变换,然后以合理的性能完成几个语句。 –

回答

3

有事情轿跑车,你可以考虑:

  1. 检查有什么用instruments tool的的time profiler采取的大部分时间。
  2. 使用UIImageView而不是UIImage来应用一些转换,如旋转和变换。您需要将这些转换应用于其UIImageView的CALayer。
  3. 使用GCD将图像加载到后台线程中。这里有一个例子:
dispatch_queue_t preloadQueue = dispatch_queue_create("preload queue", nil); 
dispatch_async(preloadQueue, ^{ 
           UIImage *yourImage = [UIImage imageNamed:yourImageReferencePath]; 
           dispatch_async(dispatch_get_main_queue(), ^{ 
                      yourUIView.image = yourImage}); 
                      }); 
dispatch_release(preloadQueue); 
+0

这是一个很好的答案! – mark

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