• iOS 图片剪切和压缩的几个方法


    // 图片剪切

    - (UIImage*)clipImageWithImage:(UIImage*)image inRect:(CGRect)rect {
        CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);

        UIGraphicsBeginImageContext(image.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, rect, imageRef);
        UIImage* clipImage = [UIImage imageWithCGImage:imageRef];
    //    CGImageCreateWithImageInRect(CGImageRef  _Nullable image, <#CGRect rect#>)
    //    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();      // 不同的方式
        UIGraphicsEndImageContext();
        
    //    NSData* data = [NSData dataWithData:UIImagePNGRepresentation(clipImage)];
    //    BOOL flag = [data writeToFile:@"/Users/gua/Desktop/Image/后.png" atomically:YES];
    //    GGLogDebug(@"========压缩后=======%@",clipImage);
        
        return clipImage;
    }

    // 图片压缩
    - (UIImage*)imageCompressImage:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth {
        CGSize imageSize = sourceImage.size;
        CGFloat width = imageSize.width;
        CGFloat height = imageSize.height;
        CGFloat targetWidth = defineWidth;
        CGFloat targetHeight = (targetWidth / width) * height;
        UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
        [sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return newImage;
    }

  • 相关阅读:
    js鼠标事件/onclick/鼠标点击/光标移开
    搭建PHP环境
    SQL语法的一些整理
    DataTime格式大全啊!
    分页存储过程
    jquery中获取键盘按键
    中国历史朝代歌(完整)
    js键盘事件
    js鼠标、键盘事件实例代码
    《.NET Compact Framework移动开发指南》答疑一
  • 原文地址:https://www.cnblogs.com/wfwenchao/p/5383215.html
Copyright © 2020-2023  润新知