美文网首页
iOS图片自适应方案组合使用笔记

iOS图片自适应方案组合使用笔记

作者: 数字d | 来源:发表于2021-11-04 14:08 被阅读0次

需求:图片宽高不确定,展示区域根据屏幕的剩余尺寸来定,可以确定的是,宽高比小于1
如果是横图,展示方式是高度不压缩,宽度截取中间部分展示
如果是竖图,也是高度不压缩,宽度截取中间和顶部展示

实现代码

    CGFloat originwidth = self.frame.size.width;
    CGFloat originheight = self.frame.size.height;
    CGFloat rateOut = originheight / originwidth;
    WS(weakSelf);
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        CGFloat innerRate = image.size.width / image.size.height;
        if (innerRate > 1) {
            //横图
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, originheight * innerRate, originheight)];
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, originheight * innerRate, originheight)];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.image = image;
            [view addSubview:imageView];

            UIImage *imageRet = [[UIImage alloc]init];
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(originheight * innerRate, originheight), YES, [UIScreen mainScreen].scale);
            [view.layer renderInContext:UIGraphicsGetCurrentContext()];
            imageRet = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            CGRect rect = CGRectMake(originheight * innerRate / 2 - originwidth / 2, 0, originwidth, originheight);
            CGFloat scale = [UIScreen mainScreen].scale;
            CGFloat x= rect.origin.x*scale,y=rect.origin.y*scale,w=rect.size.width*scale,h=rect.size.height*scale;
            CGRect dianRect = CGRectMake(x, y, w, h);
            CGImageRef sourceImageRef = [imageRet CGImage];
            CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);
            UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
            CGImageRelease(newImageRef);
            self.currentImage = newImage;
            [self judjeImageStatusWithImage];
        }else {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, originheight, originheight)];
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, originheight, originheight)];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.image = image;
            [view addSubview:imageView];

            UIImage *imageRet = [[UIImage alloc]init];
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(originheight, originheight), YES, [UIScreen mainScreen].scale);
            [view.layer renderInContext:UIGraphicsGetCurrentContext()];
            imageRet = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();


            CGRect rect = CGRectMake(originheight / 2 - originwidth / 2, 0, originwidth, originheight);
            CGFloat scale = [UIScreen mainScreen].scale;
            CGFloat x= rect.origin.x*scale,y=rect.origin.y*scale,w=rect.size.width*scale,h=rect.size.height*scale;
            CGRect dianRect = CGRectMake(x, y, w, h);
            //截取部分图片并生成新图片
            CGImageRef sourceImageRef = [imageRet CGImage];
            CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);
            UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
            CGImageRelease(newImageRef);
            self.currentImage = newImage;
            [self judjeImageStatusWithImage];
        }
    }];

相关文章

网友评论

      本文标题:iOS图片自适应方案组合使用笔记

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