IOSは写真に透かしを追加する(2つの方式)。


自分の苦労したプロジェクトが他の人に盗まれることを防止するために、ピクチャーをとって水印を追加して、ここでピクチャーの唯一無二を表します。ウォーターマークをプラスするのは上にLabelをいくつか追加するのではなくて、私達が文字をピクチャーの上で1つの全体になるので、以下の小さい編はみんなにIOSを分かち合います。
一つの方法を提供します。この方法は透かしを入れる写真と透かしの内容を伝えるだけで効果があります。
第一の方法:

-(UIImage *)watermarkImage:(UIImage *)img withName:(NSString *)name
 
 {
 
   NSString* mark = name;
 
   int w = img.size.width;
 
   int h = img.size.height;
 
   UIGraphicsBeginImageContext(img.size);
 
   [img drawInRect:CGRectMake(, , w, h)];
 
   NSDictionary *attr = @{
 
              NSFontAttributeName: [UIFont boldSystemFontOfSize:],  //    
 
              NSForegroundColorAttributeName : [UIColor redColor]   //      
 
              };
 
   [mark drawInRect:CGRectMake(, , , ) withAttributes:attr];         //   
 
   [mark drawInRect:CGRectMake(w - , , , ) withAttributes:attr];      //   
 
   [mark drawInRect:CGRectMake(w - , h - - , , ) withAttributes:attr];  //   
 
   [mark drawInRect:CGRectMake(, h - - , , ) withAttributes:attr];    //   
 
   UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
 
   UIGraphicsEndImageContext();
 
   return aimg;
 
 }
第二の方法:drawInRectを使うのはとても便利で、絵も文字もプラスできます。

//     
- (UIImage *) imageWithWaterMask:(UIImage*)mask inRect:(CGRect)rect 
{ 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 
 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) 
 { 
 UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen". 
 } 
#else 
 if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) 
 { 
 UIGraphicsBeginImageContext([self size]); 
 } 
#endif 
 //   
 [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; 
 //    
 [mask drawInRect:rect]; 
 UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext(); 
 UIGraphicsEndImageContext(); 
 return newPic; 
} 
以上述べたように、2つの方法でIOSが画像に透かしを入れることを実現しました。必要な友達は参考にしてください。皆さんが好きであることを望みます。