iOS 生成随机颜色(UIColor)
#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)的更多相关文章
- Swift - 生成随机颜色(Extension UIColor)
在开发中,我们有时需要生成一些随机的颜色.但 UIColor 没有提供方法或属性来直接获取随机颜色,这里对其进行扩展,方便使用. 1,扩展UIColor,增加随机颜色属性 1 2 3 4 5 6 7 ...
- iOS - 对UIColor颜色反差
iOS中默认的很多方法可以获得不同种颜色的UIColor对象,但是White和Black等灰度值其实是用灰阶透明度调制 +colorWithWhite:alpha:,这些CGColorRef拥有2个组 ...
- iOS涂色涂鸦效果、Swift仿喜马拉雅FM、抽屉转场动画、拖拽头像、标签选择器等源码
iOS精选源码 LeeTagView 标签选择控件 为您的用户显示界面添加美观的加载视图 Swift4: 可拖动头像,增加物理属性 Swift版抽屉效果,自定义转场动画管理器 Swift 仿写喜马拉雅 ...
- IOS Quartz2D 通过UIColor生成图片
普通生成 示例代码: //这里实现普通生成图片的方法 - (void)drawRect:(CGRect)rect { CGRect cxRect = CGRectMake(, , , ); UIGra ...
- ios 随机色 宏定义
#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1] #d ...
- iOS:扩展UIColor,支持十六进制颜色设置
来自转载:http://my.oschina.net/leejan97/blog/307491 摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换 #define UICol ...
- iOS开发:UIColor转成纯色图片(UIImage)
Objective-c 版本 UIKIT_EXTERN UIImage * __nullable UIColorAsImage(UIColor * __nonnull color, CGSize si ...
- js几种生成随机颜色方法
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- php生成随机颜色代码
function rand_color($color_array) { $color = dechex(rand(3355443,13421772)); if (in_array($color, $c ...
随机推荐
- 第三章 AOP 基于Schema的AOP
基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已. 3.7.1一般增强的使用 a.目标类 public class Target { public void ...
- swift 学习资源 大集合
今天看到一个swift学习网站,其中我们收集了大量的学习资源 Swift 介绍 Swift 介绍 来自 Apple 官方 Swift 简单介绍 (@peng_gong) 一篇不错的中文简单介绍 [译] ...
- [SQL]透過redgate SQL Monitor 來找出 ASYNC_NETWORK_IO 問題
原文:[SQL]透過redgate SQL Monitor 來找出 ASYNC_NETWORK_IO 問題 最近因為在查一個SQL的效能問題,透過 sys.dm_os_wait_stats 來取得To ...
- C++ Primer高速学习 第一章 获得二:输入和输出 (IO)
什么是输入输出.即Input-Output,缩写是非常装B的IO?请看经典民间解释: C++语言的输入输出是指信息从外部输入设备(如键盘.磁盘等)向计算机内部(内存)输入(即Input)和从内存向外单 ...
- BootStrap -- Grid System
<script src="jquery.1.9.js"></script> <script src="js/bootstrap.min.js ...
- .net机试题总结
1.下面是一个由*号组成的4行倒三角形图案.要求:1.输入倒三角形的行数,行数的取值3-21之间,对于非法的行数,要求抛出提示“非法行数!”:2.在屏幕上打印这个指定了行数的倒三角形. ******* ...
- Tomcat启动错误,端口占用
错误信息: Several ports (8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The ...
- C++ Primer 学习笔记_45_STL实践与分析(19)--建筑常规算法
STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...
- 【Spring】Spring学习笔记-01-入门级实例
听说当前Spring框架很流行,我也准备好好学学Spring开发,并将学习的过程和大家分享,希望能对志同道合的同学有所帮助. 以下是我学习Spring的第一个样例. 1.Spring开发环境的搭建 我 ...
- [渣译文] SignalR 2.0 系列: SignalR 自托管主机
原文:[渣译文] SignalR 2.0 系列: SignalR 自托管主机 英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with ASP.N ...