Text Water mark in xcode

核能气质少年 提交于 2020-01-03 06:19:07

问题


hi all i have looked at answers to similar questions and none seem to work for me. I am trying to water mark an image from the camera (image in the below) and add an image and text as a water mark. The below is working perfectly for adding the image but have no idea how to do the text.

WmarkImage = [UIImage imageNamed:@"60.png"];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[WmarkImage drawInRect:CGRectMake(image.size.width - WmarkImage.size.width, image.size.height - WmarkImage.size.height, WmarkImage.size.width, WmarkImage.size.height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imageView setImage:image];

回答1:


You should convert your text to image then merge them here is an code for this please check this.

NSString* kevin = @"Hello";
    UIFont* font = [UIFont systemFontOfSize:12.0f];
    CGSize size = [kevin sizeWithFont:font];
    // Create a bitmap context into which the text will be rendered.
    UIGraphicsBeginImageContext(size);
    // Render the text
    [kevin drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
    // Retrieve the image
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();



    UIImage *MergedImage = [UIImage imageNamed:@"mark.png"];

    CGSize newSize = CGSizeMake(200, 400);
    UIGraphicsBeginImageContext( newSize );

    // Use existing opacity as is
    [MergedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    // Apply supplied opacity if applicable
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 300, 400)];


    [imageView setImage:newImage];
    [self.view addSubview:imageView];



回答2:


This might help..

CATextLayer *theTextLayer = [CATextLayer layer];
theTextLayer.string = @"Your Text here";
theTextLayer.font = @"Helvetica";
theTextLayer.fontSize = @"12"
theTextLayer.alignmentMode = kCAAlignmentCenter;
theTextLayer.bounds = CGRectMake(0, 0, 40, 40);//give whatever width or height you want
[imageview.layer addSubLayer:theTextLayer];


来源:https://stackoverflow.com/questions/22653161/text-water-mark-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!