图片裁剪

作者: 小明讲啥故事 | 来源:发表于2019-11-30 12:00 被阅读0次
    #pragma mark -----图片裁剪------
    -(UIImage *)HU_trimImage:(UIImage *)image{
        //imageView的宽高比
        CGFloat imageViewWidthHeightRatio = _widthHeightRatio;
        if (_widthHeightRatio <= 0) {
            imageViewWidthHeightRatio = 1;
          }
        //屏幕分辨率
    //    CGFloat imageScale = [[UIScreen mainScreen] scale];
        CGFloat imageScale = 1;
        CGFloat imageWith = image.size.width*imageScale;
        CGFloat imageHeight =image.size.height*imageScale;
        //image的宽高比
        CGFloat imageWidthHeightRatio =imageWith/imageHeight;
        CGImageRef imageRef = nil;
        CGRect rect;
    
    //    NSLog(@"\nimageWith === %f\nimageHeight === %f\nImageView宽高比 == %f\nimageScale == %f",imageWith,imageHeight,imageViewWidthHeightRatio,imageScale);
        if (imageWidthHeightRatio>imageViewWidthHeightRatio) {
            rect = CGRectMake((imageWith-imageHeight*imageViewWidthHeightRatio)/2, 0, imageHeight*imageViewWidthHeightRatio, imageHeight);
        } else if (imageWidthHeightRatio<imageViewWidthHeightRatio) {
            rect = CGRectMake(0, (imageHeight-imageWith/imageViewWidthHeightRatio)/2, imageWith, imageWith/imageViewWidthHeightRatio);
        }else {
            rect = CGRectMake(0, 0, imageWith, imageHeight);
        }
    
        imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
        UIImage *res = [UIImage imageWithCGImage:imageRef scale:imageScale orientation:UIImageOrientationUp];
        /**
         一定要,千万要release,否则等着内存泄露吧,稍微高清点的图一张图就是几M内存,很快App就挂了
         */
        CGImageRelease(imageRef);
    
        return res;
    }
    

    相关文章

      网友评论

        本文标题:图片裁剪

        本文链接:https://www.haomeiwen.com/subject/jpyuvctx.html