|
| 1 | +// |
| 2 | +// QCloudPutObjectWatermarkInfo.m |
| 3 | +// Pods-QCloudCOSXMLDemo |
| 4 | +// |
| 5 | +// Created by garenwang on 2020/6/4. |
| 6 | +// |
| 7 | + |
| 8 | +#import "QCloudPicOperations.h" |
| 9 | +#import <QCloudCore/QCloudLogger.h> |
| 10 | +#import <QCloudCore/QCloudCore.h> |
| 11 | +#import "NSString+RegularExpressionCategory.h" |
| 12 | + |
| 13 | +@implementation QCloudPicOperations |
| 14 | + |
| 15 | +-(NSString *)getPicOperationsJson{ |
| 16 | + if ([self getRules] == nil) { |
| 17 | + return nil; |
| 18 | + } |
| 19 | + |
| 20 | + NSDictionary *dicOperations = @{@"is_pic_info":_is_pic_info ? @(1):@(0),@"rules":[self getRules]}; |
| 21 | + QCloudLogInfo(@"水印生成成功————%@",[dicOperations qcloud_modelToJSONString]); |
| 22 | + return [dicOperations qcloud_modelToJSONString]; |
| 23 | +} |
| 24 | + |
| 25 | +-(NSArray *)getRules{ |
| 26 | + NSMutableArray * rules = [NSMutableArray arrayWithCapacity:0]; |
| 27 | + for (QCloudPicOperationRule * item in _rule) { |
| 28 | + if (item.rule == nil ) { |
| 29 | + return nil; |
| 30 | + } |
| 31 | + |
| 32 | + if (item.fileid == nil) { |
| 33 | + QCloudLogError(@"[%@]QCloudPicOperationRule的fileid不能为空",self.class); |
| 34 | + return nil; |
| 35 | + } |
| 36 | + |
| 37 | + [rules addObject:@{@"fileid":item.fileid,@"rule":item.rule}]; |
| 38 | + } |
| 39 | + if (rules.count > 5) { |
| 40 | + return [rules subarrayWithRange:NSMakeRange(0, 5)]; |
| 41 | + } |
| 42 | + return rules.copy; |
| 43 | +} |
| 44 | + |
| 45 | +@end |
| 46 | + |
| 47 | +@implementation QCloudPicOperationRule |
| 48 | + |
| 49 | +-(NSString *)rule{ |
| 50 | + // 直接指定rule;若无指定,则按照用户输入生成rule; |
| 51 | + if (_rule != nil) { |
| 52 | + return _rule; |
| 53 | + } |
| 54 | + |
| 55 | + if (_type == 0 || _type > 3) { |
| 56 | + QCloudLogError(@"水印类型错误"); |
| 57 | + return nil; |
| 58 | + } |
| 59 | + |
| 60 | + NSMutableString * strRule = [NSMutableString stringWithString:@"watermark/3/type"]; |
| 61 | + |
| 62 | + [strRule appendFormat:@"/%ld",_type]; |
| 63 | + |
| 64 | + if (_type == QCloudPicOperationRuleFull || _type == QCloudPicOperationRuleHalf) { |
| 65 | + if (_imageURL == nil) { |
| 66 | + QCloudLogError(@"[%@]生成水印错误————半盲和全盲水印必须指定图片链接",self.class); |
| 67 | + return nil; |
| 68 | + } |
| 69 | + NSData *data = [_imageURL dataUsingEncoding:NSUTF8StringEncoding]; |
| 70 | + NSString* base64ImageUrl = [data base64EncodedStringWithOptions:0]; |
| 71 | + [strRule appendFormat:@"/image/%@",base64ImageUrl]; |
| 72 | + if (_type == QCloudPicOperationRuleFull) { |
| 73 | + if (_level < 1) { |
| 74 | + [strRule appendString:@"/level/1"]; |
| 75 | + }else if (_level > 3) { |
| 76 | + [strRule appendString:@"/level/3"]; |
| 77 | + }else{ |
| 78 | + [strRule appendFormat:@"/level/%ld",_level]; |
| 79 | + } |
| 80 | + } |
| 81 | + }else{ |
| 82 | + if (_text == nil) { |
| 83 | + QCloudLogError(@"[%@]文本型水印请传入水印文字",self.class); |
| 84 | + return nil; |
| 85 | + } |
| 86 | + |
| 87 | + if (![_text matchesRegularExpression:@"[a-zA-Z0-9]+"]) { |
| 88 | + QCloudLogError(@"[%@]文本型水印的文本仅支持[a-zA-Z0-9]",self.class); |
| 89 | + return nil; |
| 90 | + } |
| 91 | + |
| 92 | + [strRule appendFormat:@"/text/%@",_text]; |
| 93 | + } |
| 94 | + |
| 95 | + return strRule; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +@end |
| 100 | + |
| 101 | + |
0 commit comments