zoukankan      html  css  js  c++  java
  • 将image进行base64编码

    @interface NSString (Extension)

    /**

     *  将image进行base64编码

     */

    + (NSString *)imageBase64:(UIImage *)image;

    @end

    + (NSString *)imageBase64:(UIImage *)image

    {

        image = [self imageWithImageSimple:image scaledToSize:CGSizeMake(200, 200)];

        

        NSData *data =[NSData data];

        if (UIImagePNGRepresentation(image)) {

            //返回为png图像。

            data = UIImagePNGRepresentation(image);

        }else {

            //返回为JPEG图像。

            data = UIImageJPEGRepresentation(image, 0.5);

        }

        NSString *base64ImageStr = [data base64EncodedStringWithOptions:0];

        base64ImageStr = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)base64ImageStr, NULL,       

        CFSTR("="), kCFStringEncodingUTF8);

        return base64ImageStr;

    }

    /**

     *  调整发图片大小

     */

    + (UIImage *)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize;

    {

        UIGraphicsBeginImageContext(newSize);

        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

        UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return  newImage;

    }

  • 相关阅读:
    ioi1998 Polygon
    [Noip模拟题]Seq
    [noip模拟]分组行动
    入门OJ:photo
    Sgu167 I-country
    入门OJ:简单的网络游戏
    入门OJ:Coin
    ATT&CK实战系列
    Metasploit Framework(二)
    RoarCTF 2019
  • 原文地址:https://www.cnblogs.com/happyEveryData/p/5636254.html
Copyright © 2011-2022 走看看