下面是我从本地访问图像的代码,如果它不可用,那么它将下载然后显示.
[cell.imgProfilePic processImageDataWithURLString:cdata.PICTURE];
我已经制作了自定义UIImageVIEw类
DImageVIEw.h
#import <UIKit/UIKit.h> @interface DImageVIEw : UIImageVIEw @property (nonatomic,strong) UIActivityIndicatorVIEw *activityVIEw; - (voID)processImageDataWithURLString:(Nsstring *)urlString; + (UIImage *)getSavedImage :(Nsstring *)filename; @end
DImageVIEw.m
#import "DImageVIEw.h"#define IMAGES_FolDER_name @"DImages"@implementation DImageVIEw- (ID)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { } return self;}- (voID)dealloc{ self.activityVIEw = nil; [super dealloc];}- (ID)initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if (self) { [self initWithFrame:[self frame]]; } return self;}- (voID)processImageDataWithURLString:(Nsstring *)urlString{ @autoreleasepool { UIImage * saveimg =[DImageVIEw getSavedImage:urlString]; if (saveimg) { @autoreleasepool { dispatch_queue_t callerQueue = dispatch_get_main_queue(); dispatch_async(callerQueue,^{ @autoreleasepool{ [self setimage:saveimg]; } }); } } else { [self showActivityIndicator]; NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEnCoding:NSUTF8StringEnCoding]]; dispatch_queue_t callerQueue = dispatch_get_main_queue(); dispatch_queue_t downloadQueue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_HIGH,0); __block NSError* error = nil; dispatch_async(downloadQueue,^{ @autoreleasepool { NSData * imageData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error]; if (!error) { dispatch_async(callerQueue,^{ @autoreleasepool { UIImage *image = [UIImage imageWithData:imageData]; [self setimage:image]; [self hIDeActivityIndicator]; [self saveImageWithFoldername:IMAGES_FolDER_name Andfilename:urlString AndImage:imageData]; } }); } } }); dispatch_release(downloadQueue); } }}- (voID) showActivityIndicator{ self.activityVIEw = [[UIActivityIndicatorVIEw alloc]initWithFrame:CGRectMake(0,self.frame.size.wIDth,self.frame.size.height)]; self.activityVIEw.autoresizingMask = UIVIEwautoresizingFlexibleleftmargin | UIVIEwautoresizingFlexibletopmargin | UIVIEwautoresizingFlexibleRightmargin | UIVIEwautoresizingFlexibleBottommargin; self.activityVIEw.hIDesWhenStopped = TRUE; self.activityVIEw.backgroundcolor = [UIcolor clearcolor]; self.activityVIEw.activityIndicatorVIEwStyle = UIActivityIndicatorVIEwStyleGray; [self addSubvIEw:self.activityVIEw]; [self.activityVIEw startAnimating];}- (voID) hIDeActivityIndicator{ CAAnimation *animation = [NSClassFromString(@"CATransition") animation]; [animation setValue:@"kCATransitionFade" forKey:@"type"]; animation.duration = 0.4;; [self.layer addAnimation:animation forKey:nil]; [self.activityVIEw stopAnimating]; [self.activityVIEw removeFromSupervIEw]; for (UIVIEw * vIEw in self.subvIEws) { if([vIEw isKindOfClass:[UIActivityIndicatorVIEw class]]) [vIEw removeFromSupervIEw]; }}- (voID)saveImageWithFoldername:(Nsstring *)foldername Andfilename:(Nsstring *)filename AndImage:(NSData *) imageData{ @autoreleasepool{ NSfileManager *filemanger = [[NSfileManager defaultManager] autorelease]; Nsstring *directoryPath = [[Nsstring stringWithFormat:@"%@/%@",[DImageVIEw applicationdocumentsDirectory],foldername] autorelease]; if (![filemanger fileExistsAtPath:directoryPath]) { NSError *error = nil; [filemanger createDirectoryAtPath:directoryPath withIntermediateDirectorIEs:YES attributes:nil error:&error]; } filename = [DImageVIEw filenameValIDate:filename]; Nsstring *filePath = [[Nsstring stringWithFormat:@"%@/%@",directoryPath,filename] autorelease]; BOol isSaved = [imageData writetofile:filePath atomically:YES]; if (!isSaved)DLog(@" ** img Not Saved"); }}+ (Nsstring *)applicationdocumentsDirectory{ NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES); Nsstring *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; return basePath;}+ (UIImage *)getSavedImage :(Nsstring *)filename{ NSautoreleasePool* pool = [[NSautoreleasePool alloc] init]; filename = [DImageVIEw filenameValIDate:filename]; NSfileManager * filemanger = [[NSfileManager defaultManager] autorelease]; Nsstring * directoryPath = [[Nsstring stringWithFormat:@"%@/%@",[DImageVIEw applicationdocumentsDirectory],IMAGES_FolDER_name] autorelease]; Nsstring * filePath = [[Nsstring stringWithFormat:@"%@/%@",filename] autorelease]; if ([filemanger fileExistsAtPath:directoryPath]) { UIImage *image = [[[UIImage imageWithContentsOffile:filePath] retain]autorelease]; if (image) return image; else return nil; } [pool release]; return nil;}+ (Nsstring*) filenameValIDate : (Nsstring*) name{ name = [name stringByReplacingOccurrencesOfString:@"://" withString:@"##"]; name = [name stringByReplacingOccurrencesOfString:@"/" withString:@"#"]; name = [name stringByReplacingOccurrencesOfString:@"%20" withString:@""]; return name;}@end
一切正常,平滑滚动以及后台的asyncImage下载.
问题是当我滚动UItablevIEw时,应用程序内存不断增加,经过一段时间后,我得到了接收内存2/3时间然后应用程序崩溃.
当我使用AsyncImageVIEw类时,内存不会增加并且其工作正常.但由于应用程序的要求,我将所有图像保存到文档目录,并在其可用时显示.
我已尝试使用@autoreleasepool并发布一些变量但未获得成功.
我很感激,如果有人有解决方案来管理内存管理.
**ARC is off in my application.**解决方法 UIImagePNGRepresentation可能会返回非自动释放的对象 – 您可以尝试释放它并查看是否会导致崩溃.显然你没有发布一些东西,但除了图像表示之外别无其他.
其他一些评论:
>使用ObjectAlloc工具在Instruments中运行您的应用程序,并且应该立即明确哪些对象未被释放.如果您不了解乐器,那么现在是时候学习它了.
>你可以’跟踪’对象并在使用ObjectTracker解除分配时收到消息 – 但是它是为ARC设计的,所以你可能需要调整它.如果您使用它,您将在每个对象被释放时看到一条消息
>当表格视图使用单元格完成时,您可以接收一个委托方法,告诉您,然后您可以将单元格保留为nil(释放)和对象
>你对downloadQueue的使用是奇怪的 – 在你的实例中创建一次作为ivar,根据需要使用它,并在dealloc发布它
>您隐藏主队列上的活动微调器,但不要在主队列上启动它
>您命令活动视图从超级视图中删除自己,但随后在子视图中查找并尝试将其删除:
[self.activityVIEw removeFromSupervIEw];
for (UIVIEw * vIEw in self.subvIEws){ if([vIEw isKindOfClass:[UIActivityIndicatorVIEw class]]) [vIEw removeFromSupervIEw];}
最后,仪器就是你想要的.你可以在这里阅读更多关于它的信息,或者只是谷歌,你肯定会找到一大堆博客来阅读.
总结以上是内存溢出为你收集整理的ios – 当我在滚动UITableview期间显示文档目录中的图像时,内存不断增加然后应用程序崩溃全部内容,希望文章能够帮你解决ios – 当我在滚动UITableview期间显示文档目录中的图像时,内存不断增加然后应用程序崩溃所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)