【发布时间】:2014-01-03 08:25:05
【问题描述】:
我正在使用 UICollectionView 使用资产显示我的库中可用的所有图像,它显示所有图像但是当我多次上下滚动时,我在设备中收到内存问题,一段时间后它会崩溃说由于内存压力而崩溃 使用的代码如下
在这里创建collectionview并设置它的委托
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
allPhotosCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(5, 5, 310, 481)collectionViewLayout:layout];
[allPhotosCollectionView setDelegate:self];
[allPhotosCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[allPhotosCollectionView setDataSource:self];
[self.view addSubview:allPhotosCollectionView];
委托方法
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return [allImageArray count];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(95, 100);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = @"Cell";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// allimageArray has the Assest URL fo the images
NSURL *aURL=[[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@",[allImageArray objectAtIndex:indexPath.row]]];
[assestLibrary assetForURL:aURL resultBlock:^(ALAsset *asset)
{
UIImage *copyOfOriginalImager = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
NSData *imageData;
imageData=UIImageJPEGRepresentation(copyOfOriginalImager, 0.4);
[imageView setImage:[UIImage imageWithData:imageData]];
[cell.contentView addSubview:imageView];
}
failureBlock:^(NSError *error)
{
// error handling
NSLog(@"failure-----");
}];
return cell;
}
我在iOS 7中遇到很多问题,由于内存压力导致应用程序崩溃,请解释一下
【问题讨论】:
标签: ios objective-c ios7 uicollectionview didreceivememorywarning