我正在使用AFNetworking将图像(95px平方)异步加载到每个单元格上,然后当图像再次滚动到视图中时,图像从缓存中恢复(由响应代码验证为0而不是200).
这是我尝试过的:
>注释掉weakCell.photoVIEw.image = image;因此图像不会在屏幕上绘制,滚动更平滑(在http获取期间仍然有点结果)
>从cellForRowAtIndexPath方法中删除了所有AFNetworking代码,滚动更加平滑(即使屏幕上仍然绘制了自定义单元格阴影等)
>当我在屏幕上仅绘制单元格视图(带阴影)时,100个单元格的滚动非常平滑.一旦我开始在屏幕上绘制图像,我的设备上的滚动效果非常差,甚至在模拟器上也很明显. Instagram在个人资料视图中可以非常流畅地滚动数百个单元格,因此我正试图接近他们的表现.
我有什么方法可以改进下面的任何代码,以提高滚动性能?
这是我的手机代码:
#import "PhotogalleryCell.h"@implementation PhotogalleryCell- (ID)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Setup the background color,shadow,and border self.backgroundcolor = [UIcolor colorWithWhite:0.25f Alpha:1.0f]; self.layer.bordercolor = [UIcolor blackcolor].CGcolor; self.layer.borderWIDth = 0.5f; self.layer.shadowcolor = [UIcolor blackcolor].CGcolor; self.layer.shadowRadius = 3.0f; self.layer.shadowOffset = CGSizeMake(0.0f,2.0f); self.layer.shadowOpacity = 0.5f; // Make sure we rasterize for retina self.layer.rasterizationScale = [UIScreen mainScreen].scale; self.layer.shouldRasterize = YES; // Add to the content vIEw self.photoVIEw = [[UIImageVIEw alloc] initWithFrame:self.bounds]; [self.contentVIEw addSubvIEw:self.photoVIEw]; } return self;}- (voID)prepareForReuse{ [super prepareForReuse]; self.photoVIEw.image = nil; self.largeImageURL = nil;}
这是我的UICollectionVIEw代码:
#pragma mark - Collection VIEw Delegates- (NSInteger)numberOfSectionsInCollectionVIEw:(UICollectionVIEw *)collectionVIEw{ return 1;}- (NSInteger)collectionVIEw:(UICollectionVIEw *)collectionVIEw numberOfItemsInSection:(NSInteger)section{ return [zePhotos count];}- (UICollectionVIEwCell *)collectionVIEw:(UICollectionVIEw *)collectionVIEw cellForItemAtIndexPath:(NSIndexPath *)indexPath{ PhotogalleryCell *cell = [collectionVIEw dequeueReusableCellWithReuseIDentifIEr:kPGPhotoCellIDentifIEr forIndexPath:indexPath]; // Get a reference to the image dictionary NSDictionary *photoDict = [[zePhotos objectAtIndex:indexPath.row] objectForKey:@"image"]; // Asynchronously set the thumbnail vIEw __weak PhotogalleryCell *weakCell = cell; Nsstring *thumbnailURL = [[photoDict objectForKey:@"thumbnail"] objectForKey:@"url"]; NSURLRequest *photoRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thumbnailURL]]; [cell.photoVIEw setimageWithURLRequest:photoRequest placeholderImage:nil success:^(NSURLRequest *request,NShttpURLResponse *response,UIImage *image) { weakCell.photoVIEw.image = image; } failure:^(NSURLRequest *request,NSError *error) { NSLog(@"Error retrIEving thumbnail... %@",[error localizedDescription]); }]; // Cache the large image URL in case they tap on this cell later cell.largeImageURL = [[photoDict objectForKey:@"large"] objectForKey:@"url"]; return cell;}- (voID)collectionVIEw:(UICollectionVIEw *)collectionVIEw dIDSelectItemAtIndexPath:(NSIndexPath *)indexPath{ [self performSegueWithIDentifIEr:@"showPhotoDetail" sender:self];}解决方法 您可以尝试将shadowPath添加到您的单元初始化,它应该提高性能,这是我在我的一个项目中使用的代码添加一个舍入的shadowPath(有关更多选择,请参阅UIBezIErPath方法)
self.layer.shadowPath = [UIBezIErPath bezIErPathWithRoundedRect:self.frame.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10,10)].CGPath;
此外,如果我没记错AFNetworking没有调整从服务器返回的图像的大小,所以它可能会影响你的图像质量(尽管你添加到UIImageVIEw的缩放方法),我建议调度返回的图像来调整它的大小如果你想这样:
CGSize targetSize = cell.photoVIEw.bounds.size;[cell.photoVIEw setimageWithURLRequest:photoRequest placeholderImage:nil success:^(NSURLRequest *request,UIImage *image) { dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,0),^{ CGfloat imageHeight = image.size.height; CGfloat imageWIDth = image.size.wIDth; CGSize newSize = weakCell.imageVIEw.bounds.size; CGfloat scaleFactor = targetSize.wIDth / imageWIDth; newSize.height = imageHeight * scaleFactor; UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0); [image drawInRect:CGRectMake(0,newSize.wIDth,newSize.height)]; UIImage *small = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); dispatch_async(dispatch_get_main_queue(),^{ weakCell.photoVIEw.image = small; }); }); } failure:^(NSURLRequest *request,NSError *error) { NSLog(@"Error retrIEving thumbnail... %@",[error localizedDescription]); }];总结
以上是内存溢出为你收集整理的ios – 使用AFNetworking加载图像的UICollectionView滚动性能全部内容,希望文章能够帮你解决ios – 使用AFNetworking加载图像的UICollectionView滚动性能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)