iOS隨機顏色

來源:互聯網
上載者:User

標籤:

#import <UIKit/UIKit.h>@interface UIColor (RandomColor)+(UIColor *) randomColor;@end#import "UIColor+RandomColor.h"@implementation UIColor (RandomColor)+(UIColor *) randomColor{    <pre name="code" class="objc">//色相, 飽和度, 亮度
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將隨機產生的顏色繪製成圖片。-(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隨機顏色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.