iOS 选择的照片或者拍照的图片上添加日期水印
生活随笔
收集整理的這篇文章主要介紹了
iOS 选择的照片或者拍照的图片上添加日期水印
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1..引入框架 #import "CLLocation+GPSDictionary.h"
#import "NSDictionary+CLLocation.h" #import <AssetsLibrary/AssetsLibrary.h> 2. - (void) imagePickerController: (UIImagePickerController*) reader
?didFinishPickingMediaWithInfo: (NSDictionary*) info
{
??? NSString *strType = [info objectForKey:UIImagePickerControllerMediaType];
??? if ([strType isEqualToString:@"public.image"]) //當選擇的類型是圖片
??? {
??????? __block NSMutableDictionary *imageMetadata = nil;
??????? NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
??????? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
??????? [library assetForURL:assetURL
???????????????? resultBlock:^(ALAsset *asset)? {
???????????????????? imageMetadata = [[NSMutableDictionary alloc] initWithDictionary:asset.defaultRepresentation.metadata];
???????????????????? //控制臺輸出查看照片的metadata
???????????????????? self.picDataInfo = imageMetadata[@"{TIFF}"][@"DateTime"];
???????????????????? NSLog(@"%@**********", self.picDataInfo);
???????????????????? self.editeOrNot = YES;
???????????????????? UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; //先把圖片轉成NSData
???????????????????? self.image = image;
???????????????????? [reader dismissViewControllerAnimated:YES completion:nil]; //關閉相冊界面
???????????????????? self.imageView = [CRMFactory createImageViewWithFrame:CGRectMake(15, self.takePhotoButton.frame.origin.y, 60, 60) image:image];
???????????????????? [self.view addSubview:_imageView];
???????????????????? //看大圖
???????????????????? self.imageView.userInteractionEnabled = YES;
???????????????????? UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPic)];
???????????????????? [self.imageView addGestureRecognizer:tap];
????????????????????
???????????????????? self.takePhotoButton.frame = CGRectMake(15 + 60 + 15, self.takePhotoButton.frame.origin.y, 60, 60);
???????????????????? UIImage *scaleImage = [CRMDatahandle scaleFromImage:image];
???????????????????? UIImage *waterPoint = [self addText:scaleImage text:self.picDataInfo];
???????????????????? NSData *data = UIImageJPEGRepresentation(waterPoint, 1.0);
???????????????????? self.picName = [CRMDatahandle picName];
???????????????????? [self uplosaToServersice:data];
???????????????? }
??????????????? failureBlock:^(NSError *error) {
??????????????? }];
??? }
}
#pragma mark - 添加水印
- (UIImage *)addText:(UIImage *)img text:(NSString *)mark {
??? if (mark.length != 0) {
??? } else {
??????? //將時間戳轉換成時間
??????? NSDate *date = [NSDate date];
??????? //??? 限定格式
??????? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
??????? [formatter setDateFormat:@" yyyy-MM-dd? hh:mm:ss"];
??????? [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
??????? NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"china"];//時區名字或地區名字
??????? [formatter setTimeZone:timeZone];
?????? mark = [formatter stringFromDate:date];
??? }
???
??? int w = img.size.width;
??? int h = img.size.height;
??? UIGraphicsBeginImageContext(img.size);
??? [img drawInRect:CGRectMake(0, 0, w, h)];
??? NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
??? paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
??? NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:25],
??????????????????????????????? NSParagraphStyleAttributeName: paragraphStyle,
??????????????????????????????? NSForegroundColorAttributeName : [UIColor redColor],
??????????????????????????????? NSTextEffectAttributeName: NSTextEffectLetterpressStyle
??????????????????????????????? };
??? [mark drawInRect:CGRectMake(0, h - 40, w , 40) withAttributes:attribute];
??? UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
??? UIGraphicsEndImageContext();
??? return aImage; }
#import "NSDictionary+CLLocation.h" #import <AssetsLibrary/AssetsLibrary.h> 2. - (void) imagePickerController: (UIImagePickerController*) reader
?didFinishPickingMediaWithInfo: (NSDictionary*) info
{
??? NSString *strType = [info objectForKey:UIImagePickerControllerMediaType];
??? if ([strType isEqualToString:@"public.image"]) //當選擇的類型是圖片
??? {
??????? __block NSMutableDictionary *imageMetadata = nil;
??????? NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
??????? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
??????? [library assetForURL:assetURL
???????????????? resultBlock:^(ALAsset *asset)? {
???????????????????? imageMetadata = [[NSMutableDictionary alloc] initWithDictionary:asset.defaultRepresentation.metadata];
???????????????????? //控制臺輸出查看照片的metadata
???????????????????? self.picDataInfo = imageMetadata[@"{TIFF}"][@"DateTime"];
???????????????????? NSLog(@"%@**********", self.picDataInfo);
???????????????????? self.editeOrNot = YES;
???????????????????? UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; //先把圖片轉成NSData
???????????????????? self.image = image;
???????????????????? [reader dismissViewControllerAnimated:YES completion:nil]; //關閉相冊界面
???????????????????? self.imageView = [CRMFactory createImageViewWithFrame:CGRectMake(15, self.takePhotoButton.frame.origin.y, 60, 60) image:image];
???????????????????? [self.view addSubview:_imageView];
???????????????????? //看大圖
???????????????????? self.imageView.userInteractionEnabled = YES;
???????????????????? UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPic)];
???????????????????? [self.imageView addGestureRecognizer:tap];
????????????????????
???????????????????? self.takePhotoButton.frame = CGRectMake(15 + 60 + 15, self.takePhotoButton.frame.origin.y, 60, 60);
???????????????????? UIImage *scaleImage = [CRMDatahandle scaleFromImage:image];
???????????????????? UIImage *waterPoint = [self addText:scaleImage text:self.picDataInfo];
???????????????????? NSData *data = UIImageJPEGRepresentation(waterPoint, 1.0);
???????????????????? self.picName = [CRMDatahandle picName];
???????????????????? [self uplosaToServersice:data];
???????????????? }
??????????????? failureBlock:^(NSError *error) {
??????????????? }];
??? }
}
#pragma mark - 添加水印
- (UIImage *)addText:(UIImage *)img text:(NSString *)mark {
??? if (mark.length != 0) {
??? } else {
??????? //將時間戳轉換成時間
??????? NSDate *date = [NSDate date];
??????? //??? 限定格式
??????? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
??????? [formatter setDateFormat:@" yyyy-MM-dd? hh:mm:ss"];
??????? [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
??????? NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"china"];//時區名字或地區名字
??????? [formatter setTimeZone:timeZone];
?????? mark = [formatter stringFromDate:date];
??? }
???
??? int w = img.size.width;
??? int h = img.size.height;
??? UIGraphicsBeginImageContext(img.size);
??? [img drawInRect:CGRectMake(0, 0, w, h)];
??? NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
??? paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
??? NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:25],
??????????????????????????????? NSParagraphStyleAttributeName: paragraphStyle,
??????????????????????????????? NSForegroundColorAttributeName : [UIColor redColor],
??????????????????????????????? NSTextEffectAttributeName: NSTextEffectLetterpressStyle
??????????????????????????????? };
??? [mark drawInRect:CGRectMake(0, h - 40, w , 40) withAttributes:attribute];
??? UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
??? UIGraphicsEndImageContext();
??? return aImage; }
轉載于:https://www.cnblogs.com/tian-sun/p/5909953.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的iOS 选择的照片或者拍照的图片上添加日期水印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决mysqlAccess denied
- 下一篇: spring data mongo比较两