Skip to content

Instantly share code, notes, and snippets.

@nantekkotai
Created May 31, 2012 03:44
Show Gist options
  • Save nantekkotai/2840814 to your computer and use it in GitHub Desktop.
Save nantekkotai/2840814 to your computer and use it in GitHub Desktop.
UIImageの画像サイズを変更する

UIButtonにしろ、そのままイメージを使うにしろ、画像サイズを変更したい時がある。
そんな時は以下のような感じで頑張ると変更できる。

- (UIImage*)imageByShrinkingWithSize:(UIImage *)img
{
    // 元画像から8px小さくするとするじゃない
    CGRect rect = CGRectMake(0, 0,
                             img.size.width - 8,
                             img.size.height - 8);
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
    [img drawInRect:rect];
    UIImage *shrinkedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return shrinkedImage;
}

CGRectMakeに変更後の画像サイズを指定して、ごにゃごにゃするだけ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment