2

この画像レンダリング機能があり、レンダリング時にメイン/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];

}

ありがとう

4

2 に答える 2

3

あなたが考えることができるもののクーペがあります:

  1. のを使用して、ほとんどの時間を費やしているものを確認しtime profilerますinstruments tool
  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);
于 2012-09-23T21:40:17.043 に答える
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の同時実行プログラミングガイドを参照してください。

于 2012-09-23T21:38:40.823 に答える