#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. acFileStorage equivalent

    searching for a vcl that can enable embed any files within dfm similar to acfilestorage When there a ...

  2. VC中添加web控件的方法

    在VC中使用WebBrowser控件的两方法 黄森堂(vcmfc)著 ClassWizard方式: 1.创建包装类:View->ClassWizard->Add Class->For ...

  3. android binder机制之——(创建binder服务)

      Binder机制编程 前面的几篇文章具体介绍了android中binder机制的方方面面,相信你对binder机制已经有了较深刻的理解.俗话说得好"学以致用",以下我们就通过在 ...

  4. 用Python实现QQ找茬游戏外挂工具

    源地址:http://cpiz.net/blog/2012/03/a_qq_zhaocha_assistant_by_python/ (原创作品,转载请注明出处)好久没写技术相关的博文,这次写篇有意思 ...

  5. Java.util.zip adding a new file overwrites entire jar?(转)

    ZIP and TAR fomats (and the old AR format) allow file append without a full rewrite. However: The Ja ...

  6. Codechef Not a Triangle题解

    找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...

  7. NET Core 中的依赖注入

    NET Core 中的依赖注入 [共7篇] 一.控制反转(IoC) ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制, ...

  8. Shell编程中Shift的用法(转)

    位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...

  9. Company Story | Vistaprint

    Company Story | Vistaprint Company Story A Gap in the Small Business Marketplace It’s rare that a hi ...

  10. PHP开发学习门户改版效果图投票

    亲们,PHP开发学习门户上线两个月啦,站长想进行一次改版.希望大家在留下宝贵的一票.选出喜欢的样式吧 A样式: B样式: mod=misc&action=votepoll&fid=46 ...