Objective c 使用故事板制作圆形图像
我想从Facebook上制作圆形照片,但图像总是缩放的 因此,我有一个带有下一个参数的故事板: 对齐中心X,对齐中心Y,宽度等于72,高度等于72。 我明白,这可能是72/72的问题,但故事板中的图像模式是“方面匹配” 我通过URL调用我下载图像的方法,然后使用radius将其设置为角落Objective c 使用故事板制作圆形图像,objective-c,uiimage,sdwebimage,Objective C,Uiimage,Sdwebimage,我想从Facebook上制作圆形照片,但图像总是缩放的 因此,我有一个带有下一个参数的故事板: 对齐中心X,对齐中心Y,宽度等于72,高度等于72。 我明白,这可能是72/72的问题,但故事板中的图像模式是“方面匹配” 我通过URL调用我下载图像的方法,然后使用radius将其设置为角落 // call [UIImage setRoundImageView:self.p_photo WithURL:[p_user fullURL] withCornerSize:37]; + (void)s
// 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();
});
}
您的代码比需要的复杂得多。您正在将图像手动绘制到图像视图中,这是导致问题的原因。请尝试以下方法:
+ (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];
}
并确保图像视图在故事板中设置为“纵横比填充”模式。若要使图像视图的各个角落变圆,请使用此SO问题的答案:它是圆的好,麻烦了,该图像看起来是缩放的使用该SO问题中的答案使视图变圆,看看这是否解决了缩放问题。不,它像以前一样圆的好,但在比例上的主要问题,正如我所想,因为故事板,我把图像设置为宽度=72,高度=72,所以它的比例是72*72,这对我有帮助,但还有一个问题存在。当我两次调用这个方法时,第一次的角半径是35,第二次是70。我想是因为SDWebImageCache?如何确定这个问题,我可以重用缓存中的数据?唯一不同的方法是如果imageView的大小不同。您正在更改图像视图的大小,或者正在传递不同的图像视图。cornerRadius的值与SDWebImageCache无关。好的,谢谢,问题的原因在于viewWilLAppear方法,viewWilLAppear:中的值不正确,而viewDidAppear中的值正确: