search

Home  >  Q&A  >  body text

objective-c - How to adjust the size of the displayed image when loading a network image from SDWebImage

As shown in the picture: This is a screenshot of a 6s mobile phone. I put him in a UIImageView with a width = mobile phone screen width and a height of 210. This picture is obtained through network loading. Here I use SDWebImage. Now I want the image to look better, my idea is to crop this image. The problem is that the network request here is asynchronous. I don't know where to handle it.

My core problem is that I don’t know where to crop the web image loaded based on SDWebImage after it is loaded.

I want the online pictures to be displayed as follows: I already have the code for the cropping part. I just don't know where to modify it.

滿天的星座滿天的星座2798 days ago850

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-02 09:31:09

    Use the download option SDWebImageAvoidAutoSetImage, 下载完成后在后台剪裁,然后在主线程设置image并调用刷新。
    可以给 UIImageViewWrite a category for easy calling.

    UIImageView *imageView;
    
        __weak __typeof(&*imageView) weakImageView = imageView;
        [imageView sd_setImageWithURL:url
                     placeholderImage:placeholder
                              options:SDWebImageAvoidAutoSetImage // 下载完成后不要自动设置image
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                image = [image croppedImage:...]; // 后台线程剪裁图片
    
                dispatch_async(dispatch_get_main_queue(), ^{
                    __typeof(&*weakImageView) strongImageView = weakImageView;
                    if (strongImageView) {
                        strongImageView.image = image;
                        [strongImageView setNeedsLayout];
                    }
                });
            });
        }];

    reply
    0
  • Cancelreply