iOS 生成随机颜色(UIColor)
2014-05-09 11:11
501 查看
#import <UIKit/UIKit.h>
@interface UIColor (RandomColor)
+(UIColor *) randomColor;
@end
#import "UIColor+RandomColor.h"
@implementation UIColor (RandomColor)
+(UIColor *) randomColor
{
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];
@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);
}
}
}
}
@interface UIColor (RandomColor)
+(UIColor *) randomColor;
@end
#import "UIColor+RandomColor.h"
@implementation UIColor (RandomColor)
+(UIColor *) randomColor
{
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];
@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 -UIColor随机生成颜色的方法
- iOS 生成随机颜色(UIColor)
- iOS -UIColor随机生成颜色的方法
- 【iPhone代码片段之二】使用类别扩展UIColor,支持生成随机颜色
- ios随机生成200个位置不同,大小不同,颜色不同的圆
- iOS开发之UIColor类拓展,可通过上百种颜色名字生成颜色 可通过十六进制生成颜色
- 写一个UIColor的分类, 用来生成随机的颜色
- iOS 从各种效果图颜色标注生成 UIColor
- iOS开发小方法:根据UIColor一个颜色生成一张图片
- iOS --- UIColor中使用16进制选取颜色
- iOS 调试用Log()与随机颜色设置
- javascript生成随机颜色示例代码
- swift 随机生成背景颜色
- 生成随机颜色
- IOS中十六进制的颜色转换为UIColor
- IOS Quartz2D 通过UIColor生成图片
- IOS中十六进制的颜色转换为UIColor
- IOS UIColor 自定义颜色
- iOS 通过颜色来生成一个纯色图片
- Xamarin.iOS_16进制颜色转UIColor