0

から を作成しUIImageますNSAttributedString:

UIFont *font = [UIFont systemFontOfSize:12.0f];
NSDictionary *attributes = @{NSFontAttributeName:font,
                             NSForegroundColorAttributeName:[UIColor greenColor]};

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:title attributes:attributes];

// Create the image here
UIImage *image = [self imageFromAttributedString:attributedString];


#pragma mark - Get image from attributed string

- (UIImage *)imageFromAttributedString:(NSAttributedString *)text
{
    UIGraphicsBeginImageContextWithOptions(text.size, NO, 0.0);

    // draw in context
    [text drawAtPoint:CGPointMake(0.0, 0.0)];

    // transfer image
    UIImage *image = [UIGraphicsGetImageFromCurrentImageContext() imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIGraphicsEndImageContext();

    return image;
}

私の質問は、同じテキストを「反転」した画像、つまり [UIColor greenColor] を白いフォント テキストで作成する最良の方法は何ですか?

4

2 に答える 2

1

NSBackgroundColorAttributeName背景を緑にNSForegroundColorAttributeName、テキストを白に設定するために使用します。

ドキュメントには次のものがあります。

NSString *const NSForegroundColorAttributeName;
NSString *const NSBackgroundColorAttributeName;

https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/uid/TP40011688

于 2014-05-13T00:44:54.183 に答える
0

画像を取得したら、Core Image Filters を使用して、色を反転したり、青をピンクや赤などに変更したりできます。単純に色を反転する CIColorInvert という CI フィルターがありますが、個人的には colorMatrix フィルターを使用することを好みます。逆にする制御コードは次のとおりです。

このルーチンは、開始 UIImage が beginImage であり、出力が endImage であると想定していることに注意してください。

カラーマトリックスの使用:

 CIFilter *filterci = [CIFilter filterWithName:@"CIColorMatrix"  //rgreen
                                keysAndValues: kCIInputImageKey, beginImage, nil];
[filterci setValue:[CIVector vectorWithX:-1 Y:0 Z:0 W:0] forKey:@"inputRVector"]; // 5
[filterci setValue:[CIVector vectorWithX:0 Y:-1 Z:0 W:0] forKey:@"inputGVector"]; // 6
[filterci setValue:[CIVector vectorWithX:0 Y:0 Z:-1 W:0] forKey:@"inputBVector"]; // 7
[filterci setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8
[filterci setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:0] forKey:@"inputBiasVector"];     
 CIImage *boutputImagei = [filterci outputImage];

  CGImageRef cgimgi = [context createCGImage:boutputImagei fromRect:[boutputImage extent]];
UIImage *endImage = [UIImage imageWithCGImage:cgimgi];

 CGImageRelease(cgimgi);

CIColorInvert の使用:

CIFilter *filterci = [CIFilter filterWithName:@"CIColorInvert"  //rgreen
                                keysAndValues: kCIInputImageKey, beginImage, nil];
    CIImage *boutputImagei = [filterci outputImage];

  CGImageRef cgimgi = [context createCGImage:boutputImagei fromRect:[boutputImage extent]];
UIImage *endImage = [UIImage imageWithCGImage:cgimgi];

 CGImageRelease(cgimgi);
于 2014-05-13T01:15:29.410 に答える