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 ...
随机推荐
- Linux命令对应的全称解释(转)
Filesystem Hierarchy Standard(FHS) /:根目录,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者. /bin:bin 就是二进 ...
- ios发电子邮件
ios发电子邮件 by 吴雪莹 第一: NSString *myEmail = @"3423423423@qq.com"; NSString *toemail = @"a ...
- Java Web整合开发(79) -- Struts 2
一. Struts 2.x 概述 不继承任何类的Action Struts 2的Action并不一定要实现Action接口或者继承ActionSupport,任何POJO都可以做Action,只要这个 ...
- 使用SVM对于许多类型的多维数据分类
最近,我做了一件小事,使用SVM正确8三维级数据分类,在线搜索,我们发现二分的问题大家都在讨论二维数据,一些决定自己的研究.我首先参考opencvtutorial.这也是二维数据的二分类问题.然后通过 ...
- UVa 572 Oil Deposits(DFS)
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil ...
- 苹果WatchKit轻松入门
背景 前段时间苹果Apple推出 WatchKit,用于开发Apple Watch应用,同时也推出了 Xcode6.2 Beta(非稳定版,好期待稳定版)版本用于开发 Watch App.Apple ...
- IBatis.Net获取执行的Sql语句
前言 IBatis.Net中Sql语句是些在配置文件中的,而且配置文件是在程序启动时读取的(我们开发的时候需要将其设置成较新复制或者是始终复制),而不是程序将其包含在其中(例如NHibernate的映 ...
- Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock’
今天服务器遇到了一个很熟悉的问题 输入 #mysql -u root -p ERROR 2002 (HY000): Can't connect to local MySQL server throug ...
- MSXML2;System.ServiceModel.Configuration;对应dll的添加方法
- JVM内存结构、垃圾回收那点事(转)
翻看电脑的文件夹,无意看到了9月份在公司做的一次分享,浏览了一下"婆婆特",发现自己在ppt上的写的引导性问题自己也不能确切的回答出来,哎,知识这东西,平时不常用的没些日子就生疏了 ...