牵涉特别大的图片 又不准压缩展示的demo

在无线端开发中,

有时候会遇到展示一张超大图片,不允许压缩,如果传给后台。会考虑到对图片的裁剪。

对于内存的考虑。

写了一个demo,用来裁剪图片,或者展示超大图片

无线端展示:

- (void)viewDidLoad {

    [super viewDidLoad];

    [self prepareWebView];

}

- (void)prepareWebView{

    WKWebView * webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    UIImage *image = [UIImage imageNamed:@"IMG_0270.jpg"];

    NSString *imageSting = [self htmlForJPGImage:image];

    NSString *content = [NSString stringWithFormat:@"<html><body>%@</body></html>", imageSting];

    [webView loadHTMLString:content baseURL:nil];

    webView.navigationDelegate = self;

    [self.view addSubview:webView];

}


- (NSString *)htmlForJPGImage:(UIImage *)image{

    NSData *imageData = UIImageJPEGRepresentation(image,1.f);

    NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];

    return [NSString stringWithFormat:@"<div align=center><img src='%@' /></div>", imageSource];

}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation

{

    

}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler

{

    decisionHandler(WKNavigationActionPolicyAllow);

}


图片裁剪:将图片分割成固定大小的小图片标识出坐标x y 以及宽高。

- (void)setBigImage:(UIImage *)image

{

    _bigImage = image;

    self.bigImagewidth = _bigImage.size.width;

    self.bigImagehight = _bigImage.size.height;

    NSInteger j = 0;

    NSMutableArray *preceImages = [NSMutableArray array];

    while (self.bigImagewidth > (j + 1) * PieceHeight) {

       [preceImages addObjectsFromArray:[self cutImageWithOfY:j]];

        j += 1;

    }

    if (self.bigImagewidth > j * PieceHeight && self.bigImagewidth < (j + 1) * PieceHeight) {

         [preceImages addObjectsFromArray:[self cutImageWithOfY:j]];

    }

    self.pieces = preceImages;

}

- (NSArray *)cutImageWithOfY:(NSInteger )j

{

    NSInteger i = 0;

    NSMutableArray *preceImages = [NSMutableArray array];

    while (self.bigImagewidth > (i + 1) * PieceWidth ) {

        ImagePieceModel *pieceModel = [[ImagePieceModel alloc] init];

        pieceModel.width = PieceWidth;

        pieceModel.height = PieceHeight;

        pieceModel.imageX = i * PieceWidth;

        pieceModel.imageY = j * PieceHeight;

        pieceModel.image = [self cutBigImage:_bigImage wide_X:pieceModel.imageX  heigh_Y:pieceModel.imageY wideCount:PieceWidth heightCount:PieceHeight];

        [preceImages addObject:pieceModel];

        i += 1;

    }

    if (self.bigImagewidth > i * PieceWidth && self.bigImagewidth < (i + 1) * PieceWidth) {

        ImagePieceModel *pieceModel = [[ImagePieceModel alloc] init];

        pieceModel.width = self.bigImagewidth - i * PieceWidth;

        pieceModel.height = PieceHeight;

        pieceModel.imageX = i * PieceWidth;

        pieceModel.imageY = j * PieceHeight;

        pieceModel.image = [self cutBigImage:_bigImage wide_X:pieceModel.imageX  heigh_Y:pieceModel.imageY wideCount:pieceModel.width heightCount:pieceModel.height];

        [preceImages addObject:pieceModel];

    }

    return preceImages;

}

- (UIImage *)cutBigImage:(UIImage *)bigImage wide_X:(CGFloat)X  heigh_Y:(CGFloat)Y wideCount:(CGFloat)wideCount heightCount:(CGFloat)heightCount

{

    CGRect smallRect = CGRectMake(X, Y, wideCount, heightCount);

    CGImageRef smallImageRef = CGImageCreateWithImageInRect(bigImage.CGImage, smallRect);

    UIImage* smallImage = [UIImage imageWithCGImage: smallImageRef];

    return smallImage;

}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值