Add watermarks for iOS photos and for iOS photos
During the project, we need to mark the photos to prevent image theft by others. Therefore, we need to add a watermark to the photo to indicate its uniqueness.
Adding a watermark is not to add several labels on it, but to draw the text into a whole.
Provide a method. This method only needs to pass the image and the content of the watermark to achieve the effect.
1-(UIImage *) watermarkImage :( UIImage *) img withName :( NSString *) name 2 3 {4 5 NSString * mark = name; 6 7 int w = img. size. width; 8 9 int h = img. size. height; 10 11 UIGraphicsBeginImageContext (img. size); 12 13 [img drawInRect: CGRectMake (0, 0, w, h)]; 14 15 NSDictionary * attr =@{ 16 17 NSFontAttributeName: [UIFont boldSystemFontOfSize: 20], // set the font 18 19 NSForegroundColorAttributeName: [UIColor redColor] // set the font color 20 21}; 22 23 [mark drawInRect: CGRectMake (0, 10, 80, 32) withAttributes: attr]; // 24 25 [mark drawInRect: CGRectMake (w-80, 10, 80, 32) withAttributes: attr]; // 26 27 [mark drawInRect: CGRectMake (w-80, h-32-10, 80, 32) withAttributes: attr]; // 28 29 in the lower right corner [mark drawInRect: CGRectMake (0, h-32-10, 80, 32) withAttributes: attr]; // 30 31 UIImage * aimg = UIGraphicsGetImageFromCurrentImageContext (); 32 33 UIGraphicsEndImageContext (); 34 35 return aimg; 36}