Still Life

生活の記録。

影を描画したいのに

がうの絵を描いたので、早速気になるところを試したのですが、いきなりつまづきました。


キャラクターの影を描画してみたかったので、検索しながら試したのですが、惜しいところで一時退散しました。
iOS4以上と3.2とで結果が違ってしまうのです。


同じコーディングを実行した結果。
f:id:Aodrey:20110722135337j:image:w320
こちらはシミュレータで、4.0を指定。
まさにこうしたかったので、「できたー!」状態。ところが・・・


f:id:Aodrey:20110722135236j:image:w320
こちらはiPhone3Gです。
影が透過されずにまっくろです。。。


Core Graphicsについて腰を据えて取り組まないと制覇できなさそう。

参照したサイトはこちらです:(ありがとうございました)
Cocoaの日々
コードはこちらです。要は、アルファチャンネルだけを取り出して影にしたかったのです。

ShadowTestViewController.h

#import <UIKit/UIKit.h>

@interface ShadowTestViewController : UIViewController {
	IBOutlet UIImageView* gau;
	IBOutlet UIImageView* gauShadow;
}
@property(retain, nonatomic)IBOutlet UIImageView* gau;
@property(retain, nonatomic)IBOutlet UIImageView* gauShadow;

-(UIImage *)convShadowImage:(UIImage *)image;

-(UIImage*)convertGrayScaleImage:(UIImage*)image;

@end

ShadowTestViewController.m

#import "ShadowTestViewController.h"

@implementation ShadowTestViewController
@synthesize gau,gauShadow;

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.gauShadow.image = [self convShadowImage:self.gau.image];
	
}
-(UIImage *)convShadowImage:(UIImage *)image {
	
	CGRect rect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
	
	CGContextRef alphaContext = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaOnly);
	CGContextDrawImage(alphaContext, rect, [image CGImage]);
	CGImageRef alphaImage = CGBitmapContextCreateImage(alphaContext);
	CGContextRelease(alphaContext);
	
	UIImage* img = [UIImage imageWithCGImage: alphaImage];
	
	return img;
	
}
//以降略
@end

ShadowTestViewController.xib
f:id:Aodrey:20110722144303p:image