[英]100+ image files display in CollectionView with using ScrollView
我从服务器获取的每个文件夹中都有 100 多个图像。 我调整了它们的大小并在我的 Iphone 12pro 上启动它工作正常,但是当我从其他设备(即 iphone 8)加载时,会出现 memory 泄漏。 那是因为当用户单击图像时,应用程序会一次性加载所有图像。 是否有任何想法准备在单击或后台下载之前打开图像以避免 memory 泄漏?
CGFloat width = self.scrollView.frame.size.width;
CGFloat height = self.contentView.frame.size.height;
CGRect innerFrame = CGRectMake(0, 0, width, height);
__block UIImage *menuImage =[[UIImage alloc]init];
for (int i = 0; i < self.fileList.count; i++) {
// Output Image Correction////////////////////////////////////////////////////////////////////////////////////
__block UIImage* Picture = nil; // 원래의 이미지가
if ([self.ListType isEqual: @"url"]) {
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: self.fileList[i]]];
Picture = [UIImage imageWithData:imageData];
} else {
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
NSString* filePath = (NSString *)self.fileList[i];
NSString *imageFullPath = [self.filePath stringByAppendingPathComponent:filePath];
BOOL isLoadAble = [[NSFileManager defaultManager] fileExistsAtPath:imageFullPath];
if ( isLoadAble ) {
NSData *data = [NSData dataWithContentsOfFile:[self.filePath stringByAppendingPathComponent:filePath]];
Picture = [UIImage imageWithData:data];
UIGraphicsBeginImageContext(Picture.size);
CGRect rect = CGRectMake(0, 0, Picture.size.width, Picture.size.height);
[Picture drawInRect:rect];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Picture =nil;
menuImage = renderedImage;
renderedImage = nil;
CGRect frame = CGRectMake(i * width, 0, width, height);
UIImageView *imgV = [[UIImageView alloc] initWithFrame:innerFrame] ;
// New ImageSet///////////////////////////////////////////////////////////////////////////
[imgV setImage:menuImage];
menuImage = nil;
[imgV setContentMode:UIViewContentModeScaleAspectFit];
[imgV setBackgroundColor:[UIColor blackColor]];
imgV.tag = VIEW_FOR_ZOOM_TAG;
UIScrollView *psv = [[UIScrollView alloc] initWithFrame:frame];
[psv setBackgroundColor:[UIColor blackColor]];
psv.minimumZoomScale = ZOOM_MIN;
psv.maximumZoomScale = ZOOM_MAX;
psv.zoomScale = ZOOM_SCALE;
psv.contentSize = imgV.bounds.size;
psv.delegate = self.scrollDelegate;
psv.showsHorizontalScrollIndicator = NO;
psv.showsVerticalScrollIndicator = NO;
psv.contentMode = UIViewContentModeScaleAspectFit;
[psv addSubview:imgV];
[self.contentView addSubview:psv];
self.scrollView.delegate = self;
[self.pages addObject:psv];
imgV.userInteractionEnabled = YES;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.