カメラアプリ開発入門3
- 19. imageView.image = [self monochromeFilter:image];
imageView.image = [image monochromeFilter];
ViewControllerにフィルタがある場合
(前回までの記述方法)
UIImageのカテゴリの場合
13年7月27日土曜日
- 30. UIImageに画像反転機能を追加する
- (UIImage *)mirrorImage{
CGImageRef imgRef = [self CGImage]; // 画像データ取得
UIGraphicsBeginImageContext(self.size);
// コンテキスト取得
CGContextRef context = UIGraphicsGetCurrentContext();
// コンテキストの軸をXもYも等倍で反転
CGContextTranslateCTM( context, self.size.width, self.size.height); // コンテキストの原点変更
CGContextScaleCTM( context, -1.0, -1.0);
// コンテキストにイメージを描画
CGContextDrawImage( context, CGRectMake( 0, 0, self.size.width, self.size.height), imgRef);
// コンテキストからイメージを取得
UIImage *retImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return retImg;
}
13年7月27日土曜日
- 34. 画像の回転-(UIImage*)rotateImage:(int)angle{
CGImageRef imgRef = [self CGImage];
CGContextRef context;
// 角度に応じて現在のコンテキストのCTMを変更
switch (angle) {
case 90:
UIGraphicsBeginImageContext(CGSizeMake(self.size.height, self.size.width));
context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, self.size.height, self.size.width);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextRotateCTM(context, M_PI/2.0);
break;
case 180:
UIGraphicsBeginImageContext(CGSizeMake(self.size.width, self.size.height));
context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, self.size.width, 0);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextRotateCTM(context, -M_PI);
break;
case 270:
UIGraphicsBeginImageContext(CGSizeMake(self.size.height, self.size.width));
context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1.0, -1.0);
CGContextRotateCTM(context, -M_PI/2.0);
break;
default:
return self;
}
// オフスクリーンに描写->UIImage変換
CGContextDrawImage(context, CGRectMake(0, 0, self.size.width, self.size.height), imgRef);
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return retImage;
}
13年7月27日土曜日