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;
    }
  • 相关阅读:
    关于token=%1$s类似问题的说明
    Android alarmmanager的使用方法介绍
    Android setCompoundDrawablesWithIntrinsicBounds手工设置文本与图片相对位置说明
    Android Collections.sort的几种用法介绍
    android SlidingMenu属性说明
    Android 多用户模式原理和实现介绍
    Java语言中一些常用的API
    java集合框架+应用小技巧
    javaScript2
    CSS
  • 原文地址:https://www.cnblogs.com/tian-sun/p/5909953.html
Copyright © 2011-2022 走看看