问题描述
我刚刚创建了一个 UICollectionView
,用户可以将手机中的图像添加到应用中的相册功能中。我将图像保存到文档目录中的子目录中,因此可以添加和删除更多图像。但是,当我向上和向下滚动集合视图时,它非常迟钝。
I have just created a UICollectionView
in which The user can add images from their phone to the photo album feature in the app. I have the images save to the a subdirectory in the documents directory so more can be added and removed. However, when I scroll up and down the collection view, it is very laggy.
如何使滚动变得美观流畅?
How can I make the scroll nice and smooth?
我的代码:前16幅图像是预设图像之后的所有内容都来自文档目录中的子目录
My Code: The first 16 images are preset images everything after that are from a subdirectory in documents Directory
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Custom" forIndexPath:indexPath];
//Current index number
int index=indexPath.section * noOfSection + indexPath.row;
//Check if its the preset photos
if(index<16){
NSString *name=[recipePhotos objectAtIndex:indexPath.section * noOfSection + indexPath.row];
cell.imageView.image=[UIImage imageNamed:name];
}
//not preset photos, so retrieve the photos the user added
else {
NSData *data= [NSData dataWithContentsOfFile:[recipePhotos objectAtIndex:index]];
UIImage *theImage=[UIImage imageWithData:data];
cell.imageView.image=theImage;
data=nil;
}
return cell;
}
Time Profiler给了我这个
Running Time Self Symbol Name
568.0ms 63.1% 0.0 Main Thread 0x4048
320.0ms 35.5% 0.0 _pthread_start 0x405e
320.0ms 35.5% 0.0 thread_start
320.0ms 35.5% 0.0 _pthread_start
320.0ms 35.5% 0.0 0x1084be960
310.0ms 34.4% 1.0 0x1084be6f0
7.0ms 0.7% 0.0 mach_msg
2.0ms 0.2% 2.0 objc_msgSend
1.0ms 0.1% 1.0 -[NSAutoreleasePool release]
4.0ms 0.4% 0.0 _dispatch_mgr_thread 0x4052
4.0ms 0.4% 0.0 _dispatch_mgr_thread
4.0ms 0.4% 0.0 _dispatch_mgr_invoke
4.0ms 0.4% 4.0 kevent
3.0ms 0.3% 0.0 _dispatch_worker_thread2 0x62b24
3.0ms 0.3% 1.0 start_wqthread
3.0ms 0.3% 0.0 _dispatch_worker_thread2 0x62a84
3.0ms 0.3% 0.0 start_wqthread
3.0ms 0.3% 0.0 _pthread_wqthread
3.0ms 0.3% 0.0 _dispatch_worker_thread2
3.0ms 0.3% 0.0 _dispatch_queue_invoke
3.0ms 0.3% 0.0 _dispatch_queue_drain
3.0ms 0.3% 0.0 _dispatch_client_callout
2.0ms 0.2% 0.0 my_io_execute_passive_block
1.0ms 0.1% 0.0 __86-[NSPersistentUIManager writePublicPlistWithOpenWindowIDs:optionallyWaitingUntilDone:]_block_invoke_0835
1.0ms 0.1% 0.0 -[NSPersistentUIManager writePublicPlistData:]
1.0ms 0.1% 0.0 -[NSURL(NSURLPathUtilities) URLByAppendingPathComponent:]
1.0ms 0.1% 0.0 -[NSURL getResourceValue:forKey:error:]
1.0ms 0.1% 0.0 CFURLCopyResourcePropertyForKey
1.0ms 0.1% 0.0 __block_global_2
1.0ms 0.1% 0.0 -[NSPersistentUIManager writeRecords:withWindowInfos:flushingStaleData:]
1.0ms 0.1% 0.0 _dispatch_call_block_and_release
1.0ms 0.1% 0.0 0x1084b8580
1.0ms 0.1% 0.0 mach_msg_send
1.0ms 0.1% 0.0 mach_msg
1.0ms 0.1% 1.0 mach_msg_trap
1.0ms 0.1% 0.0 _pthread_struct_init 0x62a83
1.0ms 0.1% 0.0 start_wqthread
1.0ms 0.1% 0.0 _pthread_wqthread
1.0ms 0.1% 1.0 _pthread_struct_init
1.0ms 0.1% 0.0 start_wqthread 0x62a7f
推荐答案
所以在经历了一些乱七八糟的事后,我发现了问题所在是基于几个因素。
So After some messing around, I've figured out that the problem was based on a few factors.
一个 - 缩略图的图像太大了,所以我所做的是制作了一个单独的图像阵列,图像尺寸较小适合细胞。
二 - 在@ggrana的帮助下,打开一个单独的线程加快了流程,使其不那么迟钝。
三 - 我还发现拥有一组图像而不是图像位置更快 - 唯一的问题是它需要更多的内存。
One- The images for the thumbnails were too big, so what I did was made a seperate array of images with smaller image sizes that would fit the cell.Two- With the help from @ggrana, opening a seperate thread sped up the process to and made it less laggy.Three- I also found that having an array of images rather than image locations was faster-- only problem is it takes up more memory.
这篇关于UICollectionView滚动很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!