UIImageでグラデーションしたい(その1)
結論からいうと、まだできてませんがwww
とりあえず画面いっぱいに設定したUIImageViewに色を設定してみるコード。
int width = 320; int height = 480; int bytesPerRow = 4 * width; unsigned char pixel[bytesPerRow * height]; int n; unsigned char *ptr = pixel; for(int j=0;j<height;j++){ for(int i=0;i<width;i++){ *ptr++ = 128; //r *ptr++ = 0; //g *ptr++ = 255; //b *ptr++ = 255; //a } } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(pixel, width, height, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); CGImageRef newCGImg = CGBitmapContextCreateImage(ctx); CGContextRelease(ctx); CGColorSpaceRelease(colorSpace); imageView.image = [UIImage imageWithCGImage:newCGImg];
[参考]
- http://d.hatena.ne.jp/lalalila/20090201/1233441272
- http://son-son.sakura.ne.jp/programming/iphone_sdk_uiimage.html
- http://ft-lab.ne.jp/cgi-bin/wiki.cgi?page=%C6%C8%A4%EA%B8%C0%C6%FC%B5%AD%A1%CA2008%2F11%A1%CB
疑問点。
- rgbaのaの値を変更すると透過してくれるのかと思いつつ、やってみるとグレーになってしまう。これは何故なんだろ?
- kCGImageAlphaPremultipliedLast と kCGImageAlphaLast の違いは?
疑問点は後回しにして簡単なのやってみたw
for文のところをこのように変更。
for(int j=0;j<height;j++){ for(int i=0;i<width;i++){ *ptr++ = 128 * i/width * j/height; //r *ptr++ = 0 * i/width * j/height; //g *ptr++ = 255 * i/width * j/height; //b *ptr++ = 255; //a } }
UIImageでグラデーションしたい(その2)
グラデーションを使ってカラーピッカーみたいなものを作ってみた。色の値を取得するところは未実装。
[追記]色相を生成する方法が間違っていたのでソースコード含めて修正。
あと、何も考えずに実装したのでAppDelegateにメソッド詰めこんでます。
それから、アプリ名を「ImageApp」にしちゃった点は反省しますw
まずは、ImageAppAppDelegate.h
#import <UIKit/UIKit.h> @interface ImageAppAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; UIImageView *imageView; UIImageView *hueBar; UISlider *hueSlider; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UIImageView *imageView; @property (nonatomic, retain) IBOutlet UIImageView *hueBar; @property (nonatomic, retain) IBOutlet UISlider *hueSlider; -(IBAction)update:(id)sender; @end
次実装部。ImageAppAppDelegate.m
#import "ImageAppAppDelegate.h" @implementation ImageAppAppDelegate @synthesize window; @synthesize imageView; @synthesize hueSlider; @synthesize hueBar; -(CGFloat)redWithHue:(CGFloat)hue{ CGFloat n,r; n = hue * 6; if (4.0 < n && n <= 5.0) { r = n - 4; }else if(1.0 < n && n <= 2.0){ r = - n + 2; }else if(0 < n && n <= 1.0 || 5.0 < n && n <= 6.0){ r = 1; }else{ r = 0; } return r * 255; } -(CGFloat)greenWithHue:(CGFloat)hue{ CGFloat n,g; n = hue * 6; if(0 < n && n <= 1.0){ g = n; }else if (3.0 < n && n <= 4.0) { g = - n + 4; }else if (1.0 < n && n <= 3.0) { g = 1; }else{ g = 0; } return g * 255; } -(CGFloat)blueWithHue:(CGFloat)hue{ CGFloat n,b; n = hue * 6; if(2.0 < n && n <= 3.0){ b = n - 2; }else if (5.0 < n && n <= 6.0) { b = - n + 6; }else if (3.0 < n && n <= 5.0) { b = 1; }else{ b = 0; } return b * 255; } - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch [window makeKeyAndVisible]; [self update:self]; int width = hueBar.bounds.size.width; int height = hueBar.bounds.size.height; int bytesPerRow = 4 * width; unsigned char pixel[bytesPerRow * height]; unsigned char *ptr = pixel; for(int j=0;j<height;j++){ for(int i=0;i<width;i++){ *ptr++ = [self redWithHue:1.0 * i / width]; *ptr++ = [self greenWithHue:1.0 * i / width]; *ptr++ = [self blueWithHue:1.0 * i / width]; *ptr++ = 255; } } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(pixel, width, height, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); CGImageRef newCGImg = CGBitmapContextCreateImage(ctx); CGContextRelease(ctx); CGColorSpaceRelease(colorSpace); hueBar.image = [UIImage imageWithCGImage:newCGImg]; CGImageRelease(newCGImg); } -(IBAction)update:(id)sender{ int width = imageView.bounds.size.width; int height = imageView.bounds.size.height; int bytesPerRow = 4 * width; unsigned char pixel[bytesPerRow * height]; unsigned char *ptr = pixel; CGFloat r,g,b,a; CGFloat hue; hue = hueSlider.value; r = [self redWithHue:hue]; g = [self greenWithHue:hue]; b = [self blueWithHue:hue]; a = 255; CGFloat d1 = (CGFloat)1 / width; CGFloat d2; CGFloat _r,_g,_b; for(int j=0;j<height;j++){ d2 = ((CGFloat)height - j)/height; for(int i=0;i<width;i++){ _r = (255 - (255 - r) * d1 * i) * d2; _g = (255 - (255 - g) * d1 * i) * d2; _b = (255 - (255 - b) * d1 * i) * d2; *ptr++ = 255 < _r ? 255 : _r; *ptr++ = 255 < _g ? 255 : _g; *ptr++ = 255 < _b ? 255 : _b; *ptr++ = 255; } } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(pixel, width, height, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); CGImageRef newCGImg = CGBitmapContextCreateImage(ctx); CGContextRelease(ctx); CGColorSpaceRelease(colorSpace); imageView.image = [UIImage imageWithCGImage:newCGImg]; CGImageRelease(newCGImg); } - (void)dealloc { [hueBar release]; [hueSlider release]; [window release]; [super dealloc]; } @end