#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随机颜色的更多相关文章

  1. iOS 中实现随机颜色

    开发中为了测试能够快速看到效果很多时候我们对颜色采用随机颜色 代码块如下 UIColor * randomColor= [UIColor colorWithRed:((float)arc4random ...

  2. JS中取整以及随机颜色问题

    前言:感觉自己已经好久好久没有写博客了,最近都是在写在线笔记比较多.现在来到新公司了,昨天刚刚完成一个项目所以今天有空研究研究一下前端方面的技术.下午在看一个游戏代码的时候,发现了几个别人留下的不错的 ...

  3. C#产生随机颜色

    在.net Framework中提供了一个专门用来产生随机数的类System.Random.C#可以用Random产生随机的R.G.B值,从而生成随机的颜色. 对于随机数,计算机不可能产生完全随机的数 ...

  4. 采用HSV生成随机颜色

    使用hsv/hsb生成随机颜色,并排除靠近黑白两色的色值 public static String randomColor(){ int max = 25500000 ; Random rand = ...

  5. JS多种方法实现随机颜色;

    JS随机颜色有很多地方要用到:比如大家看到很多标签连接都是五颜六色.实现随机颜色的方法有多种,下面来看看具体的实现代码: 方法一: var getRandomColor = function() { ...

  6. 获取随机颜色js

    获取随机颜色方法一: function randomColor1() { var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16); i ...

  7. iOS状态栏颜色

    下面截图给出修改 iOS 状态栏颜色的 4 种方式 Target.png Info.plist.png Storyboard.png code.png 其中第四张图中的代码,直接写在你的任何一个 Vi ...

  8. echarts之词云随机颜色的配置

    echarts中的词云字体产生随机颜色,最主演的是要引入worldcloud.js,另外还要有jquery.js文件与echarts.js文件的引入,通过配置即可实现词云随机颜色的产生.下面为大家介绍 ...

  9. 【javascript】随机颜色

    调用该方法则会返回一个#xxx的rgb随机颜色 function color1(){ var sum=""; var shuzu2=['a','b','c','d','e','f' ...

随机推荐

  1. mysqldump --flush-logs

    <pre name="code" class="html"><pre name="code" class="ht ...

  2. linux 配置 mail server

    一.配置yum安装工具 ①  进入yum目录 [root@bj ~]# cd /etc/yum.repos.d ②  配置yum.repo [root@bj yum.repos.d]# cprhel- ...

  3. Eclipse插件引入jar包的方法

    搞了两天,终于找到解决办法了.原来  Eclipse 插件项目引入外面的jar包不能用   build path---->add external jars的方法. 先说明两个概念:类加载器,O ...

  4. Hibernate之1-N关联映射

    一.Hibernate之1-N关联映射 1. 哪边是 1 , 哪边是多 ?      须要从业务的角度来说明.比如,Employee 和 Department 之间就是 n-1 的关联关系,Order ...

  5. UVa 10286 - Trouble with a Pentagon

    题目:如图在正五边形中画一个正方形,一直正五边形边长,求正方形边长. 分析:计算几何,解析几何.求出边长的比例关系带入求解就可以. 设正五边形边长为1,在顶点建立直角坐标系,则左上角坐标为:B(-co ...

  6. Echart饼图、柱状图、折线图(pie、bar、line)加入点击事件

    var myChart= echarts.init(document.getElementById('myChart')); myChart.on('click', function (param) ...

  7. HDU 2255 奔小康,赚钱(KM算法模板)

    解决问题的思路: 二部图,正确的匹配,卡费用流,使用KM算法. #include <cstring> #include <algorithm> #include <cst ...

  8. CV和Resume的区别(转)

    常常有人把CV和Resume混起来称为“简历”,其实精确而言,CV应该是“履历”,Resume才是简历.Resume概述了有关的教育准备和经历,是对经验技能的摘要:curriculum vitae则集 ...

  9. Spring集成XFire开发WebService

    Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐.XFire是一个简化WebService开发的开源项目.通过Sp ...

  10. Web Api 2(Cors)Ajax跨域访问

    支持Ajax跨域访问ASP.NET Web Api 2(Cors)的简单示例教程演示   随着深入使用ASP.NET Web Api,我们可能会在项目中考虑将前端的业务分得更细.比如前端项目使用Ang ...