码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 选择的照片或者拍照的图片上添加日期水印

时间:2016-09-26 17:55:59      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

iOS 选择的照片或者拍照的图片上添加日期水印

标签:

原文地址:http://www.cnblogs.com/tian-sun/p/5909953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!