效果圖:
核心代碼:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//0.加載圖片
UIImage *image = [UIImage imageNamed:@"阿狸頭像"];
//1.開啓一個跟圖片原始大小的上下文
//opaque:不透明度
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//2.把圖片繪製到上下文當中
[image drawAtPoint:CGPointZero];
//3.把文字繪製到上下文當中
NSString *str = @"LayneCheung";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
dict[NSBackgroundColorAttributeName] = [UIColor orangeColor];
[str drawAtPoint:CGPointMake(60, 170) withAttributes:dict];
//4.從上下文當中生成一張圖片.(把上下文當中繪製的所有內容,生成一張圖片)
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5.關閉上下文.
UIGraphicsEndImageContext();
self.imageV.image = newImage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end