zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    htpasswd命令
    GitHub访问速度慢的解决方法
    easyui datagrid 首次不加载做法
    Excel日常操作
    补偿接口中循环一直执行sql的问题
    rabbitMq无法消费发送的q的问题
    Unicode与中文转换工具类方法(转)
    idea 一些插件配置
    线程安全的集合类、CopyOnWrite机制介绍(转)
    java websocket学习
  • 原文地址:https://www.cnblogs.com/tian-sun/p/5909953.html
Copyright © 2011-2022 走看看