#import <UIKit/UIKit.h>

@interface UIColor (RandomColor)

+(UIColor *) randomColor;

@end


#import "UIColor+RandomColor.h"

@implementation UIColor (RandomColor)

+(UIColor *) randomColor

{

CGFloat hue = (
arc4random() % /
256.0 );  //0.0 to 1.0

CGFloat saturation = (
arc4random() % /
256.0 ) + 0.5; 
// 0.5 to 1.0,away from white

CGFloat brightness = (
arc4random() % /
256.0 ) + 0.5; 
//0.5 to 1.0,away from black

];

@end

将随机生成的颜色绘制成图片。

-(void)createImage

{

NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)
objectAtIndex];

path = @"/Users/shouqiangwei/Desktop/未命名目录 2";

NSLog(@"path = %@",path);

NSString *imgName;

NSString *imgURL;

NSString *markNum;

UIImage *img;

NSData *imgData;

NSError *err;

, height=, fontSize;

CGPoint markPoint;

;

NSFileManager *fm = [NSFileManagerdefaultManager];

; i<; i++) {

UIColor *color = [UIColorrandomColor];

[_currentColorImageViewsetBackgroundColor:color];

imgName = [NSString
stringWithFormat:];

_lblCount.text = imgName;

imgURL = [path stringByAppendingPathComponent:imgName];

if (![fm fileExistsAtPath:imgURL]) {

const
CGFloat *colorComps = CGColorGetComponents(color.CGColor);

markNum = [NSString
stringWithFormat:];

fontSize = width / ;

markPoint = , fontSize/);

UIGraphicsBeginImageContext(CGSizeMake(width, height));

CGContextRef ctx =UIGraphicsGetCurrentContext();

], colorComps[], colorComps[], colorComps[]);

CGContextFillRect(ctx,
CGRectMake(, , 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 生成随机颜色(UIColor)的更多相关文章

  1. Swift - 生成随机颜色(Extension UIColor)

    在开发中,我们有时需要生成一些随机的颜色.但 UIColor 没有提供方法或属性来直接获取随机颜色,这里对其进行扩展,方便使用. 1,扩展UIColor,增加随机颜色属性 1 2 3 4 5 6 7 ...

  2. iOS - 对UIColor颜色反差

    iOS中默认的很多方法可以获得不同种颜色的UIColor对象,但是White和Black等灰度值其实是用灰阶透明度调制 +colorWithWhite:alpha:,这些CGColorRef拥有2个组 ...

  3. iOS涂色涂鸦效果、Swift仿喜马拉雅FM、抽屉转场动画、拖拽头像、标签选择器等源码

    iOS精选源码 LeeTagView 标签选择控件 为您的用户显示界面添加美观的加载视图 Swift4: 可拖动头像,增加物理属性 Swift版抽屉效果,自定义转场动画管理器 Swift 仿写喜马拉雅 ...

  4. IOS Quartz2D 通过UIColor生成图片

    普通生成 示例代码: //这里实现普通生成图片的方法 - (void)drawRect:(CGRect)rect { CGRect cxRect = CGRectMake(, , , ); UIGra ...

  5. ios 随机色 宏定义

    #define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1] #d ...

  6. iOS:扩展UIColor,支持十六进制颜色设置

    来自转载:http://my.oschina.net/leejan97/blog/307491 摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换 #define UICol ...

  7. iOS开发:UIColor转成纯色图片(UIImage)

    Objective-c 版本 UIKIT_EXTERN UIImage * __nullable UIColorAsImage(UIColor * __nonnull color, CGSize si ...

  8. js几种生成随机颜色方法

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. php生成随机颜色代码

    function rand_color($color_array) { $color = dechex(rand(3355443,13421772)); if (in_array($color, $c ...

随机推荐

  1. HTML中心在页面上弹出自定义表单层(实现可能拖累)

    使用DIV窗体来动态显示内容的原理:首先採用CSS和HTML隐藏弹窗中的内容,然后利用JavaScript(本教程中是JQuery)来动态显示它们.这样的效果不仅可以充分利用有限的版面空间,并且可以提 ...

  2. C#遍历FTP文件夹/下载

    原文链接:http://blog.csdn.net/ou8811/article/details/5295780 整个程序大致可以分为2个部分,第一部分是实现单个文件下载的方法 [c-sharp] v ...

  3. ASP.Net 重写IHttpModule 来拦截 HttpApplication 实现HTML资源压缩和空白过滤

    务实直接上代码: 1. 重写FilterModule.cs using System; using System.Collections.Generic; using System.Linq; usi ...

  4. Blend4精选案例图解教程(三):一键拖拽

    原文:Blend4精选案例图解教程(三):一键拖拽 拖拽效果,常规实现方法是定义MoveLeftDwon.MoveLeftUp.MouseMove事件,在Blend的世界里,实现对象的拖拽,可以不写一 ...

  5. 【SICP读书笔记(二)】使用过程来黏合数据 —— 酷炫吊的消息传递机制

    首先,让我们来看几个内建函数 (cons x y),作用是把x和y绑定成一个序对 (car z),作用是提取z序对的第一个元素 (cdr z),作用是提取z序对的第二个元素 容易看出,这个东西有点类似 ...

  6. 升级到cocos2d-x 2.0.2代码差异

    来自:http://www.cnblogs.com/TopWin/archive/2012/09/12/2682042.html 近期看cocos2d-x 2.0.2公布后升级了一下.升级后发现又出现 ...

  7. 同TextView在不同的显示内容

    首先,请原谅我不能命名文章.. . 我们不能准确地表达你说说什么什么,真正急着赶智商. 直接在地图上 如图所看到的显示的是两个textview 第一个实现的是,在同一个textview中给不同内容赋予 ...

  8. js多物体任意值运动

    假如有两个div,一个div要变宽,一个div要变高,你怎么写呢? 哎呀,写2个方法啊,一个控制div1变宽,一个控制div2变高啊 那么你的代码,是不是下面这样的呢! 示例:Div变宽和变高 现象: ...

  9. Mybatis简单的入门之增删改查

    一般的过程例如以下 1.加入Mybatis所须要的包,和连接数据库所需的包 2.配置mybatis-config.xml文件 3.配置与pojo相应的映射文件 mybatis-config,xml & ...

  10. UVALive 5103 Computer Virus on Planet Pandora Description 一些新兴需求模式的字符串 AC自己主动机

    主题链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=3104">点击打开链接 题意: ...