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 } }