iOS 随机颜色(UIColor)

本文介绍了一种使用Objective-C编程语言实现随机颜色生成,并将其应用于图片绘制的技术。通过生成不同随机颜色,程序能够创建丰富多彩的图片,展示了颜色生成与图片处理的基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import @interface UIColor (RandomColor) (UIColor *) randomColor; @end #import "UIColorRandomColor.h" @implementation UIColor (RandomColor) (UIColor *) randomColor { CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0 CGFloat satura

#import

copyright www.stuhack.com
@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 www.stuhack.com
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 

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

copyright www.stuhack.com
-(void)createImage
{ copyright www.stuhack.com

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++) {

www.stuhack.com
UIColor *color = [UIColorrandomColor]; 
[_currentColorImageViewsetBackgroundColor:color]; www.stuhack.com

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; copyright www.stuhack.com
UIGraphicsEndImageContext();


//创建到本地。
imgData = [NSDatadataWithData:UIImageJPEGRepresentation(img,1.0f)];

[imgData writeToFile:imgURLoptions:NSDataWritingAtomicerror:&err];

if (err) {
NSLog(@"error: %@\nimage:%@", err.localizedDescription, imgName);
} else {
NSLog(@"done: %@", imgName);
}
}
}

}

转载于:https://www.cnblogs.com/qq449832375/p/4672303.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值