码迷,mamicode.com
首页 > 其他好文 > 详细

通过颜色生成图片

时间:2016-05-02 22:57:18      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

/**
 *  通过颜色生成纯颜色的图片
 */

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.window.rootViewController = [[RootViewController alloc] init];
    
    [self.window makeKeyAndVisible];
    return YES;
}


@end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"
#import "UIImage+CreatImageWithColor.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UIImage *image = [UIImage imageWithColor:[UIColor redColor] imageWidth:100 imageWithHeight:100];
    UIImage *image = [UIImage imageWithColor:[UIColor yellowColor]];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(100, 100, 200, 200);
    [self.view addSubview:imageView];
}



@end
#import <UIKit/UIKit.h>

@interface UIImage (CreatImageWithColor)

/**
 *  通过颜色生成该纯颜色的图片
 *
 *  @param color 生成图片的颜色
 *
 *  @return 返回图片
 */
+ (UIImage *)imageWithColor:(UIColor *)color;

/**
 *  通过颜色生成该纯颜色的图片
 *
 *  @param color  生成图片的颜色
 *  @param width  生成图片的宽
 *  @param height 生成图片的高
 *
 *  @return 返回图片
 */
+ (UIImage *)imageWithColor:(UIColor *)color imageWidth:(CGFloat)width imageWithHeight:(CGFloat)height;

@end
#import "UIImage+CreatImageWithColor.h"

@implementation UIImage (CreatImageWithColor)

+ (UIImage *)imageWithColor:(UIColor *)color{
    return [UIImage imageWithColor:color imageWidth:100 imageWithHeight:100];
}

+ (UIImage *)imageWithColor:(UIColor *)color imageWidth:(CGFloat)width imageWithHeight:(CGFloat)height{
    
    //开启基于位图的图形上下文
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height), NO, 0.0);
    // 设置画笔的颜色
    [color set];
    // 画矩形,并填充
    UIRectFill(CGRectMake(0, 0, width, height));
    //获取图片
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    
    return resultImage;
}

@end

 

通过颜色生成图片

标签:

原文地址:http://www.cnblogs.com/lantu1989/p/5453269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!