• 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 / 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: %@ image:%@", err.localizedDescription, imgName);

                } else {

                    NSLog(@"done: %@", imgName);

                }

            }

        }


    }




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

  • 相关阅读:
    理清可导、可微、连续...的概念
    f(sinx)到底是啥
    高数基础知识整理7.渐近线与曲率
    高数基础知识整理6.函数单调性与凹凸性
    高数基础知识整理4.导数与微分
    数组、稀疏矩阵、广义表综合应用
    数据库(外键及其约束理解)
    js中按钮去触发定时器,那么多次点击这个定时器会越来越快,解决方法
    JavaScript splice() 方法和JavaScript split() 方法
    javascript中加号(+)操作符的作用
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4658763.html
Copyright © 2020-2023  润新知