我添加了一个简单的集合视图,我添加了从后端获取的项目,当我将所有对象添加到nsmutablearray时,它占用的内存更少但是当我在滚动集合视图时将获取的对象添加到NSObject时需要更多的内存
这是一个示例代码
myObjectModel.h
#import <Foundation/Foundation.h>
@interface myObjectModel: NSObject
@property (nonatomic, strong) NSString *objectTitle;
@property (nonatomic, strong) NSString *objectDesc;
@property (nonatomic, strong) NSString *objectImgURL;
- (id)initWithObjectTitle:(NSString *)dTitle
objectImgURL:(NSString *)dObjectImgURL
objectDesc:(NSString *)dObjectDesc;
@end
myObjectModel.m
#import "myObjectModel.h"
@implementation myObjectModel
- (id)initWithObjectTitle:(NSString *)dTitle
objectImgURL:(NSString *)dObjectImgURL
objectDesc:(NSString *)dObjectDesc {
self = [super init];
if (self) {
self.objectTitle = dTitle;
self.objectImgURL = dObjectImgURL;
self.objectDesc = dObjectDesc;
}
return self;
}
@end;
然后我将这个标题导入我的UIViewController,它有一个collectionview,同时获取所有对象,我添加这个方法来添加这些对象,然后将它们保存到nsmutablearray
BackendlessDataQuery *query = [BackendlessDataQuery query];
query.queryOptions.pageSize = @(100); //set page size
[[backendless.persistenceService of:[Channels class]] find:query response:^(BackendlessCollection *backendlessObjects) {
NSArray *currentPage = [backendlessObjects getCurrentPage];
NSMutableArray *arrData = [[NSMutableArray alloc]init];
for (Objects *objects in currentPage) {
@autoreleasepool {
myObjectModel *dataItem = [[myObjectModel alloc]initWithObjectTitle:backendlessObjects.title
objectImgURL:backendlessObjects.image
objectDesc:backendlessObjects.Desc;
[arrData addObject:dataItem];
}
//Test is a nsarray added in the header file
self.test = arrData;
[self.collectionView reloadData];
} error:^(Fault *fault) {
}];
这是我添加到我的收藏数据源
的内容#pragma mark - UICollectionViewDatasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView; {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [test count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
myCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"data" forIndexPath:indexPath];
myObjectModel *dataItem = (myObjectModel*)[test objectAtIndex:indexPath.row];
// cell.objectTitle.text = dataItem.objectTitle;
[cell.channelImg sd_setImageWithURL:[NSURL URLWithString:dataItem.objectImgURL] placeholderImage:[UIImage imageNamed:@"NoImage.png"]];
return cell;
}
当我运行此代码时,所有对象都将出现在collectionview中,但滚动时内存不断增加。
答案 0 :(得分:0)
没有理由通过使用测试阵列增加内存,原因是使用此
[cell.channelImg sd_setImageWithURL:[NSURL URLWithString:dataItem.objectImgURL] placeholderImage:[UIImage imageNamed:@"NoImage.png"]];
这将从URL异步下载图像。如果您在上面注释,那么它可能不会增加内存。