UITableViewCell、UICollectionViewCell中使用,卡顿更严重了
yujieace opened this issue · comments
RT,使用传统方式设置圆角,iPhone 7帧率约57fps,使用这个类库设置圆角,帧率直接降低至50fps以下,画面产生明显卡顿。
仔细查看代码,发现通过对image的观察者实现替换操作,但是,这个image的裁剪操作是在当前线程完成的,也就是说设置image是在主线程完成的操作,这个裁剪也是主线程完成的,我不知道作者是基于什么考虑在主线程做这个操作的,并且这个操作并没有缓存,也就是说,每次设置都要裁剪。这导致相当严重的性能问题。
我感觉也是变卡了。。。58变54,52
实际上可以使用YYImage来进行圆角优化,这个库我试验过了,确实没什么用。每次设置图片都会裁剪,无论是否已经裁剪过,也没有良好的缓存机制来保障性能。建议使用YYImage来裁剪圆角。话说这么多的star哪来的,用过的应该都会感觉到卡顿严重的。
确实,裁剪圆角没问题,不过都在主线程操作,也没有考虑缓存,在TableView中确实不合适
修改一下,用着还行。
-
(void)zy_cornerRadiusWithImage:(UIImage *)image cornerRadius:(CGFloat)cornerRadius rectCornerType:(UIRectCorner)rectCornerType {
CGFloat scale = [UIScreen mainScreen].scale;
CGSize cornerRadii = CGSizeMake(cornerRadius, cornerRadius);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIGraphicsBeginImageContextWithOptions(image.size, NO, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -image.size.height);
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:rectCornerType cornerRadii:cornerRadii];
[cornerPath closePath];
CGContextSaveGState(context);
[cornerPath addClip];
[self drawBorder:cornerPath];
CGContextDrawImage(context, rect, image.CGImage);
CGContextRestoreGState(context);
UIImage * processedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();dispatch_async(dispatch_get_main_queue(), ^{ if (processedImage) { objc_setAssociatedObject(processedImage, &kProcessedImage, @(1), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } self.image = processedImage; });
});
}