UIImageView需要时间加载图像
我有一些问题,我想从特定的链接下载图像下载这个图像发生在其他类是singlton类名moreAppInternet.m
我的更多AppInterNet.m文件如下
// finishedImgDling是bool
// mutableData是NSMutableData
// urlConnection是NSURLConnection
// appimage是UIImage
// imageUrl是NSString包含链接
+(id)sharedManager {
static moreAppInterNet *sharedMyManager = nil;
@synchronized(self) {
if (sharedMyManager == nil)
sharedMyManager = [[self alloc] init];
}
return sharedMyManager;
}
- (void)downloadImageFromUrl {
finishedImgDling = NO;
[self.mutableData setLength:0];
self.urlConnection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.imageUrl]] delegate:self];
while(!finishedImgDling) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
- (void)连接:(NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)response {
if (connection == self.urlConnection)
{
NSLog(@"imageDling");
}
}
- (void)连接:(NSURLConnection *)连接didReceiveData:(NSData *)data {
if (connection == self.urlConnection)
{
if (self.mutableData == nil)
{
self.mutableData = [NSMutableData data];
}
[self.mutableData appendData:data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection == self.urlConnection)
{
NSLog(@"image Downloaded");
finishedImgDling = YES;
[self setUrlConnection:nil];
[self storeImage];
}
}
- (void)storeImage {
[self setAppimage:nil];
self.appimage = [UIImage imageWithData:self.mutableData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"appImage.png"]];
[fileManager createFileAtPath:path contents:UIImagePNGRepresentation(self.appimage) attributes:nil];
NSLog(@"downloading and storing complete");
}
和我的其他ViewController是从我称之为该功能的地方
- (void)viewDidLoad {
[moreAppInterNet sharedManager];
[self downloadImage];
}
- (void)downloadImage {
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download_Image_from_other_class) object:nil];
NSOperationQueue *que = [[NSOperationQueue alloc] init];
[que addObserver:self forKeyPath:@"operations" options:0 context:NULL];
[que addOperation:operation];
[operation release];
self.dlingQue = que;
[que release];
}
- (void)download_Image_from_other_class {
[[moreAppInterNet sharedManager] downloadImageFromUrl];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)对象更改:(NSDictionary *)更改上下文:(void *)context {
if ([keyPath isEqualToString:@"operations"] && object == self.dlingQue)
{
if ([self.dlingQue operationCount] == 0)
{
self.imgView = [[moreAppInterNet sharedManager] appimage];
//it takes lots of time to display image in UIImageView
//Even though Above line get executed
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
}
问题是self.imgView需要大量的时间来加载图像大约5秒,即使它完全下载
任何人都可以帮助我或告诉我我做错了什么
我用于联网的图书馆是AFNetworking
我认为你的代码在你请求时再次下载图像。
您需要检查downloadImageFromUrl
是否有该文件。
可能需要更强大的库来处理文件下载。
链接地址: http://www.djcxy.com/p/28173.html上一篇: UIImageView Takes Time To Load Image
下一篇: saving changed text in textview loaded from NSMutableDictionary