標籤:++ begin net 傾斜 ddc article ios7 ios htm
圖片加浮水印效果的實現並儲存至相冊
實現效果
project下載:githubproject下載連結
代碼:
- (void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@"pushu.jpg"]; UIImage *waterImage = [self waterMarkImage:image withText:@"樸樹浮水印測試"]; UIImageWriteToSavedPhotosAlbum(waterImage, nil, nil, nil); //儲存圖片至相冊// 展示圖片 UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; imageView.image = waterImage; [self.view addSubview:imageView]; // Do any additional setup after loading the view, typically from a nib.}
- (UIImage *)waterMarkImage:(UIImage *)image withText:(NSString *)text { UIGraphicsBeginImageContext(image.size);// 在畫布中繪製內容 [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];// 繪製文字 [[UIColor darkGrayColor] set]; CGRect rect = CGRectMake(70, 220, 200, 60); NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:30], NSObliquenessAttributeName:@1}; //這裡設定了字型,和傾斜度。具體其它參數文章結尾有具體說明的文章連結 [text drawInRect:rect withAttributes:dic]; //在iOS7之前用下列方法比較方便// [text drawInRect:rect withFont:[UIFont systemFontOfSize:30] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];// 從畫布中得到image UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return returnImage;}
- (void)drawInRect:(CGRect)rect withAttributes:(NSDictionary *)attrs NS_AVAILABLE_IOS(7_0);
關於此方法,在iOS7以後應用。attrs設定參數的具體說明在例如以下連結文章中:
說明文章連結:attrs參數說明文章
iOS圖片加浮水印效果的實現並儲存至相冊