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' ...
随机推荐
- Android本地视频播放器开发--视频解码
在上一章Android本地视频播放器开发--SDL编译编译中编译出sdl的支持库,当时我们使用的2.0,但是有些api被更改了,所以在以下的使用者中我们使用SDL1.3的库,这个库我会传上源码以及编译 ...
- protobuf(Protocol Buffers)java初体验
因为项目须要所以简单的研究了下protobuf.我也是參照网上的博客,所以大部分内容我也就不反复造轮子了.首先protobuf介绍点击这里,使用介绍点击这里,使用demo看这里. 我个人的第一个样例也 ...
- JS Bin Tips and Bits • About
JS Bin Tips and Bits • About About Who built this? JS Bin was built by Remy Sharp and is completel ...
- 《powershell 的版本号所引起的载入 FSharp 编译器问题》基本解决
<powershell 的版本号所引起的载入 FSharp 编译器问题>基本解决 1.FSharp.Core.dll.不光要 Add-Type,还要在编译中引用.可是,在 VS2012 的 ...
- GNU GPL介绍
怎样在程序中使用GNU许可证 不管使用哪种许可证,使用时须要在每一个程序的源文件里加入两个元素:一个版权声明和一个复制许可声明.说明该程序使用GNU许可证进行授权.另外在声明版权前应该说明 ...
- thinkPHP四种URL访问方式(二)
原文:thinkPHP四种URL访问方式(二) 四.url的4种访问方式 1.PATHINFO 模式 -- (重点) http://域名/项目名/入口文件/模块名/方法名/键1/值1/键2/ ...
- SVNKit getFileFromSVN
/* * ==================================================================== * Copyright (c) 2004-2011 ...
- Android自己定义控件:老版优酷的三级菜单(效果图 + Demo)
效果图: 制作思路: 1.先分析这个效果,事实上能够理解为把三级菜单分成level1,level2,level3,level1是始终显示的. 点击level1后,level2会出现:点击level2后 ...
- GitHub 优秀Android 开源项目
阅读目录 1.Xabber客户端 2.oschina客户端 3.手机安全管家 4.星座连萌 5.玲闹铃 6.魔乐盒 7.PWP日历 8.Apollo音乐播放器 9.夏普名片识别 10.高仿人人网 11 ...
- 一张图总结Google C++编程规范(Google C++ Style Guide)
Google C++ Style Guide是一份不错的C++编码指南,我制作了一张比較全面的说明图,能够在短时间内高速掌握规范的重点内容.只是规范毕竟是人定的,记得活学活用.看图前别忘了阅读以下三条 ...