15

私は画像処理に非常に慣れていません。iPhoneアプリケーションプロジェクトに色相効果を実装する必要があります。したがって、の色相を変更する必要がありUIImageます。このためのサンプルコードまたはチュートリアルリンクを教えてください。

前もって感謝します

4

6 に答える 6

42

まず、画像を固定の色相にするか、既存の色相を回転させるかを決定する必要があります。両方の例を以下に示します。

色相の変化

固定色相

色相の修正は、標準の描画ツールを使用して行うことができます。ブレンドモードがに設定されていることを確認kCGBlendModeHueし、適切な色相で描画してください。これはNitishのソリューションから導き出されたものですが、結果が一貫していないsaturationことがわかりました。1.0この方法ではアルファを変更できますが、100%以外の真の飽和状態の場合は、おそらく2回目の描画が必要になります。

- (UIImage*) imageWithImage:(UIImage*) source fixedHue:(CGFloat) hue alpha:(CGFloat) alpha;
// Note: the hue input ranges from 0.0 to 1.0, both red.  Values outside this range will be clamped to 0.0 or 1.0.
{
    // Find the image dimensions.
    CGSize imageSize = [source size];
    CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);

    // Create a context containing the image.
    UIGraphicsBeginImageContext(imageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();    
    [source drawAtPoint:CGPointMake(0,0)];

    // Draw the hue on top of the image.
    CGContextSetBlendMode(context, kCGBlendModeHue);
    [[UIColor colorWithHue:hue saturation:1.0 brightness:1 alpha:alpha] set];
    UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
    [imagePath fill];

    // Retrieve the new image.
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return result;
}

回転した色相

色相を回転させるには、CoreImageフィルターが必要です。以下のコードは、UIImageをCIImageに変換してから、結果をUIImageに変換して表示します。画像の出所によっては、これらの手順の一方または両方を回避できる場合があります。

便利なことに、 Core Imageプログラミングガイドの最初の例: CoreImageFiltersの使用ではCIHueAdjustフィルターを使用します。

// #import <CoreImage/CoreImage.h>
// Partially from https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/CoreImaging/ci_tasks/ci_tasks.html
- (UIImage*) imageWithImage:(UIImage*) source rotatedByHue:(CGFloat) deltaHueRadians;
{
    // Create a Core Image version of the image.
    CIImage *sourceCore = [CIImage imageWithCGImage:[source CGImage]];

    // Apply a CIHueAdjust filter
    CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
    [hueAdjust setDefaults];
    [hueAdjust setValue: sourceCore forKey: @"inputImage"];
    [hueAdjust setValue: [NSNumber numberWithFloat: deltaHueRadians] forKey: @"inputAngle"];
    CIImage *resultCore = [hueAdjust valueForKey: @"outputImage"];

    // Convert the filter output back into a UIImage.
    // This section from http://stackoverflow.com/a/7797578/1318452
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef resultRef = [context createCGImage:resultCore fromRect:[resultCore extent]];
    UIImage *result = [UIImage imageWithCGImage:resultRef];
    CGImageRelease(resultRef);

    return result;
}
于 2012-07-19T09:13:16.693 に答える
3
- (void) changeToHue:(float)hue saturation:(float)saturation {
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();    
    UIView *hueBlend = [[UIView alloc] initWithFrame:self.bounds];
    hueBlend.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:1 alpha:1];
    CGContextDrawImage(context, self.bounds, self.image.CGImage);
    CGContextSetBlendMode(context, kCGBlendModeHue);
    [hueBlend.layer renderInContext:context];
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
}
于 2012-07-19T06:39:04.983 に答える
3

Apple の Swift ブログで、この写真フィルタリング アプリのチュートリアルを見つけました: https://developer.apple.com/swift/blog/?id=16

以下は、ランダムな色相が適用された UIImage を返す包含メソッドを作成するために編集されたサンプル コードです。

func applyHue(source: UIImage) -> UIImage {
    // Create a place to render the filtered image
    let context = CIContext(options: nil)

    // Create an image to filter
    let inputImage = CIImage(image: source)

    // Create a random color to pass to a filter
    let randomColor = [kCIInputAngleKey: (Double(arc4random_uniform(314)) / 100)]

    // Apply a filter to the image
    let filteredImage = inputImage!.imageByApplyingFilter("CIHueAdjust", withInputParameters: randomColor)

    // Render the filtered image
    let renderedImage = context.createCGImage(filteredImage, fromRect: filteredImage.extent)

    // Return a UIImage
    return UIImage(CGImage: renderedImage)
}
于 2016-05-18T06:31:36.903 に答える
2

Dondragmer の優れた固定色相の例を出発点として取り上げます。これが私の修正です。透明な領域が同じ色相で設定される問題を修正します。

- (UIImage*) imageWithImage:(NSString *)name fixedHue:(CGFloat) hue alpha:(CGFloat) alpha;
// Note: the hue input ranges from 0.0 to 1.0, both red.  Values outside this range will be clamped to 0.0 or 1.0.
{
    UIImage *source = [UIImage imageNamed:name];

    // Find the image dimensions.
    CGSize imageSize = [source size];
    CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);

    // Create a context containing the image.
    UIGraphicsBeginImageContext(imageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [source drawAtPoint:CGPointMake(0,0)];

    // Setup a clip region using the image
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0, source.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextClipToMask(context, imageExtent, source.CGImage);

    //[[UIColor colorWithHue:hue saturation:1.0 brightness:1 alpha:alpha] set];
    CGContextFillRect(context, imageExtent);

    // Draw the hue on top of the image.
    CGContextDrawImage(context, imageExtent, source.CGImage);
    CGContextSetBlendMode(context, kCGBlendModeHue);
    [[UIColor colorWithHue:hue saturation:1.0 brightness:1 alpha:alpha] set];
    UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
    [imagePath fill];

    CGContextRestoreGState(context); // remove clip region

    // Retrieve the new image.
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}

おかげで:

透明な領域を持つUIIMAGEの色相を変更するには?

CGContextClipToMask UIImage マスク 上下反転

于 2015-03-15T12:36:37.063 に答える
2

上記の Dondragmer の回転コードの Swift バージョン

// Convert the filter output back into a UIImage.
// This section from http://stackoverflow.com/a/7797578/1318452
func imageWithImage(source: UIImage, rotatedByHue: CGFloat) -> UIImage {
    // Create a Core Image version of the image.
    let sourceCore = CIImage(CGImage: source.CGImage)
    // Apply a CIHueAdjust filter
    let hueAdjust = CIFilter(name: "CIHueAdjust")
    hueAdjust.setDefaults()
    hueAdjust.setValue(sourceCore, forKey: "inputImage")
    hueAdjust.setValue(CGFloat(rotatedByHue), forKey: "inputAngle")
    let resultCore  = hueAdjust.valueForKey("outputImage") as CIImage!
    let context = CIContext(options: nil)
    let resultRef = context.createCGImage(resultCore, fromRect: resultCore!.extent())
    let result = UIImage(CGImage: resultRef)
    return result!
}
于 2015-02-17T05:03:24.787 に答える