BKAsciiImage 0.1.1

BKAsciiImage 0.1.1

测试已测试
Lang语言 Obj-CObjective C
许可 MIT
Released上次发布2015年4月

Barış Koç 维护。



  • 作者
  • Barış Koç

Example gif image

如 Cmd.fm iOS 应用所示

https://itunes.apple.com/app/cmd.fm-radio-for-geeks-hackers/id935765356

Cmd.fm screenshot 1 Cmd.fm screenshot 2

安装

BKAsciiImage 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:

pod "BKAsciiImage"

用法

使用 BKAsciiConverter 类

导入 BKAsciiConverter 头文件

#import <BKAsciiImage/BKAsciiConverter.h>

创建一个 BKAsciiConverter 实例

BKAsciiConverter *converter = [BKAsciiConverter new];

同步转换

UIImage *inputImage = [UIImage imageNamed:@"anImage"];
UIImage *asciiImage = [converter convertImage:inputImage];

后台转换,提供一个完成块。完成块将在主线程上被调用。

[converter convertImage:self.inputImage completionHandler:^(UIImage *asciiImage) {
    // do whatever you want with the resulting asciiImage
}];

转换为 NSString

NSLog(@"%@",[converter convertToString:self.inputImage]);

// asynchronous
[converter convertToString:self.inputImage completionHandler:^(NSString *asciiString) {
    NSLog(@"%@",asciiString);
}];

转换器选项

converter.backgroundColor = [UIColor whiteColor]; // default: Clear color. Image background is transparent
converter.grayscale = YES; // default: NO
converter.font = [UIFont fontWithName:@"Monaco" size:13.0]; // default: System font of size 10
converter.reversedLuminance = NO; // Reverses the luminance mapping. Reversing gives better results on a dark bg. default: YES
converter.columns = 50; // By default columns is derived by the font size if not set explicitly

使用 UIImage 分类

导入头文件

#import <BKAsciiImage/UIImage+BKAscii.h>

使用提供的分类方法

UIImage *inputImage = [UIImage imageNamed:@"anImage"];
[inputImage bk_asciiImageCompletionHandler:^(UIImage *asciiImage) {

}];

[inputImage bk_asciiStringCompletionHandler:^(NSString *asciiString) {

}];

[inputImage bk_asciiImageWithFont: [UIFont fontWithName:@"Monaco" size:13.0]
                          bgColor: [UIColor redColor];
                          columns: 30
                         reversed: YES
                        grayscale: NO
                completionHandler: ^(NSString *asciiString) {
                    // do whatever you want with the resulting asciiImage
                }];

高级用法

默认情况下,亮度值通过以下方式映射到字符串:

NSDictionary *dictionary = @{  @1.0: @" ",
                               @0.95:@"`",
                               @0.92:@".",
                               @0.9 :@",",
                               @0.8 :@"-",
                               @0.75:@"~",
                               @0.7 :@"+",
                               @0.65:@"<",
                               @0.6 :@">",
                               @0.55:@"o",
                               @0.5 :@"=",
                               @0.35:@"*",
                               @0.3 :@"%",
                               @0.1 :@"X",
                               @0.0 :@"@"
                               };

您可以使用自己的映射字典来实例化一个转换器

NSDictionary *dictionary = @{  @1.0: @" ",
                               @0.7 :@"a",
                               @0.65:@"b",
                               @0.6 :@"c",
                               @0.55:@"d",
                               @0.5 :@"e",
                               @0.35:@"f",
                               @0.3 :@"g",
                               @0.1 :@" ",
                               @0.0 :@" "
                               };



BKAsciiConverter *converter = [[BKAsciiConverter alloc] initWithDictionary:dictionary];
UIImage *inputImage = [UIImage imageNamed:@"anImage"];
UIImage *asciiImage = [converter convertImage:inputImage];

Mapping example screenshot

作者

Barış Koç, https://github.com/bkoc

许可

BKAsciiImage 可在 MIT 许可下获得。有关更多信息,请参阅 LICENSE 文件。