IOS random color, ios

Source: Internet
Author: User

IOS random color, ios

# Import <UIKit/UIKit. h> @ interface UIColor (RandomColor) + (UIColor *) randomColor; @ end # import "UIColor + RandomColor. h "@ implementation UIColor (RandomColor) + (UIColor *) randomColor {<pre name =" code "class =" objc "> // color, saturation, and brightness
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];
//        //RGB//        CGFloat red = arc4random() % 256 / 255.0;//        CGFloat green = arc4random() % 256 / 255.0;//        CGFloat blue = arc4random() % 256 / 255.0;        <pre name="code" class="objc"><pre name="code" class="objc">//        <span style="font-family: Arial, Helvetica, sans-serif;">return </span><span style="font-family: Arial, Helvetica, sans-serif;"> [UIColor colorWithRed:red green:green blue:blue alpha:1];</span>
 
 

@ End: draws the randomly generated color into an image. -(Void) createImage {NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; path = @ "/Users/shouqiangwei/Desktop/unnamed folder 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; NSFileManag Er * 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 ); values (CGSizeMake (width, height); CGContextRef ctx = UIGraphicsGetCurrentContext (); CGContextSetRGBFillColor (ctx, colorComps [0], colorComps [1], colorComps [2], col OrComps [3]); CGContextFillRect (ctx, CGRectMake (0, 0, width, height); CGContextSetRGBFillColor (ctx, 1.0, 1.0, 1.0, 1.0 ); // create a number on the image. [MarkNum drawAtPoint: markPoint withFont: [UIFont systemFontOfSize: fontSize]; img = upper (); self. imageView. image = img; UIGraphicsEndImageContext (); // create it locally. ImgData = [NSDatadataWithData: UIImageJPEGRepresentation (img, 1.0f)]; [imgData writeToFile: imgURLoptions: NSDataWritingAtomicerror: & err]; if (err) {NSLog (@ "error: % @ \ nimage: % @ ", err. localizedDescription, imgName);} else {NSLog (@ "done: % @", imgName );}}}}
 


In ios development, how do I randomly extract 8 different images? How can I use code to achieve random extraction?

1. Put your picture in NSArray
2. Generate a random number ranging from 0 to 7 (do I need to teach this? I won't check it on Google. It's very easy)
3. Use the random number as the index Image * img = [array objectAtIndex: index];

The object color I created is a color that cannot be randomly changed. How can I adjust it? The specified random color has been hooked up.

1. Icon → option → General → layer default settings ("new node defaults to layer by layer" is not checked), and then click the Color Block in "Name and color" of the modify panel, in the displayed "object color" box, select "Assign random color" to achieve random color allocation;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.