【发布时间】:2014-01-20 04:09:08
【问题描述】:
我正在从核心数据中将图像添加到集合视图中,该核心数据从 uiimagepicker 中获取图像。但是,当我将超过 6 张图像加载到我的应用程序中时,它会占用大部分内存,有时还会崩溃。除了获取它们并将它们直接放入具有 100x100 图像视图的单元格中的集合视图之外,我没有对图像做任何事情。有谁知道我可以做些什么来减少内存并使应用程序不会崩溃?
代码sn-ps:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _fetchedObjects.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PhotosCollectionViewCell * cell = (PhotosCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
NSManagedObject *object = [_fetchedObjects objectAtIndex:indexPath.row];
cell.cellImage.image = [UIImage imageWithData:[object valueForKey:@"image"]];
return cell;
}
其他类:
Photo * photoEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:_context];
NSError * error;
NSData *imageData = UIImagePNGRepresentation(_image);
photoEntity.name = _nameField.text;
photoEntity.image = imageData;
photoEntity.imagedescription = _descriptionField.text;
if (![_context save:&error]) {
NSLog(@"There was a save error:%@",error);
}
[self dismissViewControllerAnimated:YES completion:nil];
谢谢
【问题讨论】:
-
存储的图像大小是多少?如果图像视图为 100 x 100,则显示时需要缩小图像尺寸。
-
@iCoder 我希望在您单击图像时仍然保持更高的分辨率。可以两者兼得吗?
-
我已经回答了同样的问题 [这里][1] [1]:stackoverflow.com/questions/17161755/…希望这会有所帮助!
-
你的图片是什么格式的,尺寸是多少?
标签: ios objective-c xcode core-data uicollectionview