2012-09-23 26 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