iOS随机颜色
#import <UIKit/UIKit.h> @interface UIColor (RandomColor)
+(UIColor *) randomColor;
@end #import "UIColor+RandomColor.h" @implementation UIColor (RandomColor) +(UIColor *) randomColor
{
<pre name="code" class="objc">//色相, 饱和度, 亮度
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; <pre name="code" class="objc"><pre name="code" class="objc">// <span style="font-family: Arial, Helvetica, sans-serif;">return </span><span style="font-family: Arial, Helvetica, sans-serif;"> [UIColor colorWithRed:red green:green blue:blue alpha:1];</span>
@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); } } }}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
iOS随机颜色的更多相关文章
- iOS 中实现随机颜色
开发中为了测试能够快速看到效果很多时候我们对颜色采用随机颜色 代码块如下 UIColor * randomColor= [UIColor colorWithRed:((float)arc4random ...
- JS中取整以及随机颜色问题
前言:感觉自己已经好久好久没有写博客了,最近都是在写在线笔记比较多.现在来到新公司了,昨天刚刚完成一个项目所以今天有空研究研究一下前端方面的技术.下午在看一个游戏代码的时候,发现了几个别人留下的不错的 ...
- C#产生随机颜色
在.net Framework中提供了一个专门用来产生随机数的类System.Random.C#可以用Random产生随机的R.G.B值,从而生成随机的颜色. 对于随机数,计算机不可能产生完全随机的数 ...
- 采用HSV生成随机颜色
使用hsv/hsb生成随机颜色,并排除靠近黑白两色的色值 public static String randomColor(){ int max = 25500000 ; Random rand = ...
- JS多种方法实现随机颜色;
JS随机颜色有很多地方要用到:比如大家看到很多标签连接都是五颜六色.实现随机颜色的方法有多种,下面来看看具体的实现代码: 方法一: var getRandomColor = function() { ...
- 获取随机颜色js
获取随机颜色方法一: function randomColor1() { var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16); i ...
- iOS状态栏颜色
下面截图给出修改 iOS 状态栏颜色的 4 种方式 Target.png Info.plist.png Storyboard.png code.png 其中第四张图中的代码,直接写在你的任何一个 Vi ...
- echarts之词云随机颜色的配置
echarts中的词云字体产生随机颜色,最主演的是要引入worldcloud.js,另外还要有jquery.js文件与echarts.js文件的引入,通过配置即可实现词云随机颜色的产生.下面为大家介绍 ...
- 【javascript】随机颜色
调用该方法则会返回一个#xxx的rgb随机颜色 function color1(){ var sum=""; var shuzu2=['a','b','c','d','e','f' ...
随机推荐
- vc 在edit控件中动态插入数据滚动显示
内存从网上论坛摘抄整理 思路:给控件设置多行属性,设置垂直滚动条,Auto Vscroll设置为true,放入文本后把插入点设置到末尾 pEdit->LineScroll(pEdit->G ...
- 使用Elasticsearch、Logstash、Kibana与Redis(作为缓冲区)对Nginx日志进行收集(转)
摘要 使用Elasticsearch.Logstash.Kibana与Redis(作为缓冲区)对Nginx日志进行收集 版本 elasticsearch版本: elasticsearch-2.2.0 ...
- Tair是一个高性能,分布式,可扩展,高可靠的key/value结构存储系统(转)
Tair是一个高性能,分布式,可扩展,高可靠的key/value结构存储系统! Tair专为小文件优化,并提供简单易用的接口(类似Map)Tair支持Java和C版本的客户端 Tair is a di ...
- Swift - 类初始化和反初始化方法(init与deinit)
1,init():类的初始化方法(构造方法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...
- Redis Destop Manager不能访问虚拟机
虚拟机centOS中安装Redis,主机Redis Destop Manager不能访问虚拟机Redis server的解决方案 今天在学些redis的时候碰到个问题,发现主机Redis Destop ...
- C#对HTTP数据还原
使用C#对HTTP数据还原 [创建时间:2016-05-12 00:19:00] NetAnalyzer下载地址 在NetAnalyzer2016中加入了一个HTTP分析功能,很过用户对此都很感兴 ...
- Python天天美味(25) - 深入理解yield
Python天天美味(25) - 深入理解yield - CoderZh - 博客园 Python天天美味(25) - 深入理解yield yield的英文单词意思是生产,刚接触Python的时候 ...
- HDU 1007 近期点对
分治法求近期点对 递归将点不断分成小组.计算最短距离.此时的最短距离不过两点都属两块的某一块(这里的切割点是mid点). 还须要考虑两点分属两块的情况. 这时对于选点则把范围缩小到了以mid为中心. ...
- HNCU1099:堆积木
http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1099 题目描述 小明的侄子要过生日了,小 ...
- Loser tree in Python | Christan Christens
Loser tree in Python | Christan Christens Loser tree in Python I am taking an Advanced Data Structur ...