使用故事板制作圆形图像

时间:2014-11-30 14:15:43

标签: objective-c uiimage sdwebimage

我想从Facebook制作圆形照片,但图像总是会缩放。

所以我有下一个参数的故事板: http://prntscr.com/5bpuqy

对齐中心X,对齐中心Y,宽度等于72,高度等于72。 我明白,这个问题可能在72/72,但故事板中的图像模式是“Aspect Fit”

我通过URL调用我的方法来下载图像,然后用radius进行逼近。

// call 
[UIImage setRoundImageView:self.p_photo WithURL:[p_user fullURL] withCornerSize:37];

+ (void)setRoundImageView:(UIImageView *)imageView WithURL:(NSURL *)url withCornerSize:(CGFloat)corner
    {
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

        [[SDWebImageManager sharedManager] downloadImageWithURL:url
                                                        options:0
                                                       progress:^(NSInteger receivedSize, NSInteger expectedSize) {

                                                       } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

                                                           if (image && finished)
                                                           {
                                                               [self setRoundImage:image forImageView:imageView withCornerSize:corner];
                                                           }
                                                       }];
    });
 }

+ (void)setRoundImage:(UIImage *)image forImageView:(UIImageView *)imageView withCornerSize:(CGFloat)corner
{
    dispatch_async(dispatch_get_main_queue(), ^{

        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
        [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
                                    cornerRadius:corner] addClip];
        [image drawInRect:imageView.bounds];
        imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    });
}

1 个答案:

答案 0 :(得分:1)

您的代码比它需要的更复杂。您正在手动将图像绘制到图像视图中,这是导致问题的原因。试试这个:

+ (void)setRoundImageView:(UIImageView *)imageView WithURL:(NSURL *)url
{
    // you don't need to wrap this in a dispatch queue, SDWebImageManager takes care of that for you.
    [[SDWebImageManager sharedManager] downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if (image && finished) {
            [self setRoundImage:image forImageView:imageView];
        }
    }];
}

+ (void)setRoundImage:(UIImage *)image forImageView:(UIImageView *)imageView
{
    // you don't need to wrap this in a dispatch queue, it will be called on the main thread.
    // you don't need to manually draw the image into the image view. Just add the image to the image view and let it do its thing.
    imageView.layer.cornerRadius = CGRectGetHeight(imageView.bounds) / 2;
    imageView.layer.masksToBounds = YES;
    [imageView setImage:image];
}

并确保图像视图设置为" Aspect Fill"故事板中的模式。