如何在开发iOS App中使用SDWebImage修剪下载图像的中心部分

时间:2014-08-31 17:02:11

标签: ios objective-c uiimageview uiimage sdwebimage

我正在使用SDWebImage开发iOS App。

我想从下载的图像中修剪中央100px * 100px并在UIImageView中设置它。

我正在写下以下代码,但不是修剪过的图像,而是在NewsImage中设置了原始下载的图像。

    UIImageView *NewsImage = (UIImageView *)[cell viewWithTag:1];
    [NewsImage setImageWithURL:imageURL
              placeholderImage:placeholderImage
                       options:SDWebImageProgressiveDownload
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                         int imageW = image.size.width;
                         int imageH = image.size.height;

                         int posX = (imageW - 100) / 2;
                         int posY = (imageH - 100) / 2;
                         CGRect trimArea = CGRectMake(posX, posY, 100, 100);

                         CGImageRef srcImageRef = [image CGImage];
                         CGImageRef trimmedImageRef = CGImageCreateWithImageInRect(srcImageRef, trimArea);
                         image = [UIImage imageWithCGImage:trimmedImageRef];
                        }];

你能告诉我如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

UIImageView *NewsImage = (UIImageView *)[cell viewWithTag:1];
    [NewsImage setImageWithURL:imageURL
              placeholderImage:placeholderImage
                       options:SDWebImageProgressiveDownload
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                         float imageW = image.size.width;
                         float imageH = image.size.height;

                         float posX = (imageW - 100) / 2;
                         float posY = (imageH - 100) / 2;
                         CGRect trimArea = CGRectMake(posX, posY, 100, 100);

                         CGImageRef srcImageRef = [image CGImage];
                         CGImageRef trimmedImageRef = CGImageCreateWithImageInRect(srcImageRef, trimArea);
                         image = [UIImage imageWithCGImage:trimmedImageRef];
                         NewsImage.image = image;
                         CGImageRelease(imageRef);
                        }];