2012-05-09 9 views
9

私は2つの画像を持っています。最初はユーザーの個人用画像、2番目はアイコン(バッジ)です。別のUIImageの上にUIImageを追加します。

私は最初のuiimage(ユーザーの画像)の左下隅に2番目のuiimage(アイコン)を追加して、新しいUiimageに保存したいと考えています。

おかげ

答えて

27

を:

-(UIImage *)drawImage:(UIImage*)profileImage withBadge:(UIImage *)badge 
{ 
    UIGraphicsBeginImageContextWithOptions(profileImage.size, NO, 0.0f); 
    [profileImage drawInRect:CGRectMake(0, 0, profileImage.size.width, profileImage.size.height)]; 
    [badge drawInRect:CGRectMake(0, profileImage.size.height - badge.size.height, badge.size.width, badge.size.height)]; 
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return resultImage; 
} 

あなたはでそれを使用することができます:

UIImage *myBadgedImage = [self drawImage:profileImage withBadge:badgeImage]; 
+0

コードは、特定のかかるだろう、なぜすべてのアイデアをUIImagesを開いてそれらを拡大する - 本質的に行為を失う実際のサイズ?バッジなしでプロフィール画像を渡すだけであっても、画像が拡大表示されているので、このコードは機能しません。 – Praxiteles

+0

保存しました。私は、このヘルパーを使用する代わりに、UIImageViewsを別のものの上に積み重ねたものに戻す必要はなかった –

2

このお試しください:この方法を試してみてください

CGFloat scale = [self currentScale]; 
if (scale > 1.5) 
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, scale); 
else 
    UIGraphicsBeginImageContext(view.frame.size); 

[image1 drawInRect:CGRectMake(0, 0, w1, h1)]; 
[image2 drawInRect:CGRectMake(0, 0, w2, h2)]; 

UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
関連する問題