2017-12-02 3 views
0

現在、以下の関数を使用してpngイメージの色を変更しています。カラースライダで色が設定されていますので、色が滑らかになり、結果的に色が変化します。滑りながらスライダーの性能に問題があるだけでなく、画像の色の更新と同様に遅れを保ち、プロセスを円滑にするために助けが必要です。Objective-Cイメージのカラーパフォーマンスを変更する

- (UIImage*)imageWithImage:(UIImage *)sourceImage fixedHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha{ 
    CGSize imageSize = [sourceImage size]; 
    UIGraphicsBeginImageContext(imageSize); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(context, 0, sourceImage.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CGRect rect = CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height); 

    CGContextSetBlendMode(context, kCGBlendModeNormal); 
    CGContextDrawImage(context, rect, sourceImage.CGImage); 
    CGContextSetBlendMode(context, kCGBlendModeColor); 
    [[UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha] setFill]; 
    CGContextFillRect(context, rect); 
    CGContextSetBlendMode(context, kCGBlendModeDestinationIn); 
    CGContextDrawImage(context, rect, sourceImage.CGImage); 
    CGContextFlush(context); 
    UIImage *editedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return editedImage; 
} 

答えて

0

次のように

- (void)imageWithImage:(UIImage *)sourceImage 
       fixedHue:(CGFloat)hue 
      saturation:(CGFloat)saturation 
      brightness:(CGFloat)brightness 
       alpha:(CGFloat)alpha 
      completion:(void (^)(UIImage *))completion { 

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(queue, ^{ 
     // call your original function. Use this to create the context... 
     UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0.0); 
     // don't call CGContextFillRect, call... 
     UIRectFill(rect); 
     // don't call CGContextDrawImage, call... 
     [sourceImage drawInRect:rect] 
     // don't call CGContextFlush, don't need to replace that 

     UIImage *image = [self imageWithImage:sourceImage fixedHue:hue saturation:saturation brightness:brightness alpha:alpha]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      completion(image); 
     }); 
    }); 
} 

はこのようにそれを使用して...あなたの関数の非同期バージョンを作成します。

- (IBAction)sliderValueChanged:(UISlider *)sender { 
    [self imageWithImage:sourceImage 
       fixedHue:hue 
       saturation:saturation 
       brightness:brightness 
        alpha:alpha 
       completion:^(UIImage *image) { 
        // update the UI here with image 
       }]; 
} 
+0

は、このライン上のアプリがクラッシュ CGContextFillRect(あなたにDanhありがとうコンテキスト、rect); は、UIスレッドで実装する必要がありますが、メインスレッドのディスパッチにこの行を追加しました。まだ運がありません。 – Development

+0

ああ、申し訳ありません。現在のコンテキストを取得しないでください。一つ作る。数分で編集を投稿します。 – danh

+0

申し訳ありません - 元のコードを丁寧に読んでいます。あなたはCGContextを作成しています。メインから外して実行する必要があるすべての変更が見つかったと思います(UIアクションは禁止されています)。 – danh

関連する問題