#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);

}

}

}

}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

04-21 05:30