zoukankan      html  css  js  c++  java
  • ios图片加水印或文字

    1.加文字

    -(UIImage *)addText:(UIImage *)img text:(NSString *)text1
    {
        //get image width and height
        int w = img.size.width;
        int h = img.size.height;
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        //create a graphic context with CGBitmapContextCreate
        CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
        CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
        CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1);
        char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
        CGContextSelectFont(context, "Georgia", 30, kCGEncodingMacRoman);
        CGContextSetTextDrawingMode(context, kCGTextFill);
        CGContextSetRGBFillColor(context, 255, 0, 0, 1);
        CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2, text, strlen(text));
        //Create image ref from the context
        CGImageRef imageMasked = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        return [UIImage imageWithCGImage:imageMasked];
    }

    2.加图片

    -(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo
    {
        //get image width and height
        int w = img.size.width;
        int h = img.size.height;
        int logoWidth = logo.size.width;
        int logoHeight = logo.size.height;
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        
        //create a graphic context with CGBitmapContextCreate
        CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
        CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
        CGContextDrawImage(context, CGRectMake(w-logoWidth, 0, logoWidth, logoHeight), [logo CGImage]);
        CGImageRef imageMasked = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        return [UIImage imageWithCGImage:imageMasked];
        //  CGContextDrawImage(contextRef, CGRectMake(100, 50, 200, 80), [smallImg CGImage]);
    }

    ps:转自http://www.oschina.net/code/snippet_54100_4071

  • 相关阅读:
    完美串(区间dp)
    Brackets(区间dp)
    Eureka的高可用
    在Spring Boot中使用 @ConfigurationProperties 注解
    Spring Boot干货系列:(四)开发Web应用之Thymeleaf篇
    luogu3707 相关分析 (线段树)
    luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)
    bzoj4504 K个串 (优先队列+主席树)
    bzoj4336 骑士的旅行 (树链剖分+multiset)
    suoi37 清点更多船只 (卡空间线段树)
  • 原文地址:https://www.cnblogs.com/phnix/p/2673895.html
Copyright © 2011-2022 走看看