LoginSignup
28
26

More than 5 years have passed since last update.

UIImageを少し暗くしたUIImageを生成する

Last updated at Posted at 2013-03-12

FrogApps 技術ブログ始めました!
RailsやiOS、HTML5の情報を発信中!! → http://qiita.com/teams/frogapps


ボタンやセルを押した時の画像として、元のUIImageを少し暗くしたUIImageが欲しいことがあります。
それ用にPNGファイルを用意するのはアプリのサイズが増えてしまうので、暗くしたUIImageを生成するメソッドを作成しました。

darken.m
- (UIImage *)darken:(UIImage*)image {
    CGRect rect = CGRectMake(0.0f,0.0f,image.size.width,image.size.height);

    UIGraphicsBeginImageContextWithOptions(image.size,NO,0);
    [image drawInRect:rect];
    [image drawInRect:rect blendMode:kCGBlendModeDifference alpha:0.3f];
    UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return blendedImage;
}

元画像にkCGBlendModeDifferenceで同じ画像をブレンドしています。ポイントは元画像を2回使っていることで、透過部分は透過のままになってくれます。

これでお手軽に暗い画像を生成できます。細かい制御はできないけど、アルファ値でおおざっぱに暗さを指定できます。

28
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
26