博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS随机颜色
阅读量:7117 次
发布时间:2019-06-28

本文共 2507 字,大约阅读时间需要 8 分钟。

#import 
@interface UIColor (RandomColor)+(UIColor *) randomColor;@end#import "UIColor+RandomColor.h"@implementation UIColor (RandomColor)+(UIColor *) randomColor{
//色相, 饱和度, 亮度
CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0 CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
//        //RGB//        CGFloat red = arc4random() % 256 / 255.0;//        CGFloat green = arc4random() % 256 / 255.0;//        CGFloat blue = arc4random() % 256 / 255.0;        
//        return  [UIColor colorWithRed:red green:green blue:blue alpha:1];
 
 
@end将随机生成的颜色绘制成图片。-(void)createImage{ NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0]; path = @"/Users/shouqiangwei/Desktop/未命名目录 2"; NSLog(@"path = %@",path); NSString *imgName; NSString *imgURL; NSString *markNum; UIImage *img; NSData *imgData; NSError *err; int width= 1000, height=1000, fontSize; CGPoint markPoint; int baseLength = 320; NSFileManager *fm = [NSFileManagerdefaultManager]; for (int i=0; i<1000; i++) { UIColor *color = [UIColorrandomColor]; [_currentColorImageViewsetBackgroundColor:color]; imgName = [NSString stringWithFormat:@"%i.jpg", i+1]; _lblCount.text = imgName; imgURL = [path stringByAppendingPathComponent:imgName]; if (![fm fileExistsAtPath:imgURL]) { const CGFloat *colorComps = CGColorGetComponents(color.CGColor); markNum = [NSString stringWithFormat:@"%i", i+1]; fontSize = width / 5; markPoint = CGPointMake(fontSize/2, fontSize/2); UIGraphicsBeginImageContext(CGSizeMake(width, height)); CGContextRef ctx =UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(ctx, colorComps[0], colorComps[1], colorComps[2], colorComps[3]); CGContextFillRect(ctx, CGRectMake(0, 0, width, height)); CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0,1.0); //创建图片上的数字。 [markNum drawAtPoint:markPoint withFont:[UIFont systemFontOfSize:fontSize]]; img = UIGraphicsGetImageFromCurrentImageContext(); self.imageView.image= img; UIGraphicsEndImageContext(); //创建到本地。 imgData = [NSDatadataWithData:UIImageJPEGRepresentation(img,1.0f)]; [imgData writeToFile:imgURLoptions:NSDataWritingAtomicerror:&err]; if (err) { NSLog(@"error: %@\nimage:%@", err.localizedDescription, imgName); } else { NSLog(@"done: %@", imgName); } } }}
 

版权声明:本文博客原创文章。博客,未经同意,不得转载。

你可能感兴趣的文章
JavaScript 小数取整的函数
查看>>
Django 学习资料
查看>>
CountDownLatch多任务并行处理
查看>>
矩阵乘法
查看>>
Unicode(统一码、万国码、单一码)
查看>>
【协变】和【逆变】的简单示例
查看>>
Zabbix实现短信报警设置(实战)
查看>>
Windows下Git多账号配置,同一电脑多个ssh-key的管理
查看>>
微信打开网址添加在浏览器中打开提示遮罩
查看>>
maven 打包 java中的配置文件
查看>>
13.生成规格文件
查看>>
maven学习手记 - 1
查看>>
DLL基本开发规则
查看>>
前端进阶-Position
查看>>
PDO和PDOStatement类常用方法
查看>>
JS - The react framework
查看>>
在js自定义函数中使用$(event.target)代替$(this)
查看>>
[HNOI2006]最短母串问题——AC自动机+状压+bfs环形处理
查看>>
Codechef MARCH14 GERALD07加强版
查看>>
React-Apollo-Prisma-Graphql
查看>>