【发布时间】:2014-06-01 11:31:48
【问题描述】:
我尝试为存储图像创建一个图像控制器,并在需要时在一个应用程序实例中重用它,这是我的课程 [注:方法 controllaDictionary 搜索图像的键,如果是刚刚使用(上面详细解释)]
-(void)imageContentUrl:(NSString*)imageURL andImage:(UIImage**)image{
NSLog(@"Stampa Immagine Contente URL: %@",imageURL);
NSString* imageName = [[imageURL componentsSeparatedByString:@"/"] lastObject];
if([self controllaDictionary:imageName]){
NSLog(@"||| IMMAGES ---------> Image Exixt in Dictionary");
UIImage* imageInDict = [self controllaDictionary:imageName];
image = &imageInDict;
return;
}else{
UIImage* imageFile = nil;
if([imageName isEqualToString:@""] || imageName == nil){
NSLog(@"||| IMMAGES ------- Null Link");
image = nil;
}
if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",_imageDirectory,imageName]]){
NSLog(@"||| IMMAGES ------- Exist in Folder: %@",[NSString stringWithFormat:@"%@/%@",_imageDirectory,imageName]);
imageFile = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",_imageDirectory,imageName]];
image = &imageFile;
[self.imageDict setValue:imageFile forKey:imageName];
return;
}else{
NSLog(@"||| IMMAGINI ------- Miss, Start Download at URL: %@",imageURL);
[self downloadImageAtUrl:imageURL andImagePointer:image withHendlerBlock:^(NSError *error, UIImage *image, NSString *urlImage,UIImage** doubleImagePointer) {
if (!error) {
NSLog(@"Stampa Double Pointer IMG -> %@",image);
doubleImagePointer = ℑ
[self saveImage:image andName:imageName];
[self.imageDict setValue:image forKey:imageName];
}else{
NSLog(@"Stampa Errore: %@",error);
}
}];
}
}
}
-(void)downloadImageAtUrl:(NSString*)urlImage andImagePointer:(UIImage**)image withHendlerBlock:(void(^)(NSError* error,UIImage* imageDownloaded,NSString* urlImage,UIImage** doubleImagePointer))dowloadImageBlock{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
UIImage* downloadImg = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlImage]]];
NSError* error = nil;
if (downloadImg) {
NSLog(@"Immagine Acquisita");
}else{
NSLog(@"Errore Download Compilazione Errore");
error = [NSError errorWithDomain:DownloadErrorDomain code:0 userInfo:@{DownloadErrorDomain: @"Errore nel download dell'immagine, non si hanno a disposizione ulteriori strumenti per comprenderne la natura. Effettuare un ceck della URL"}];
}
dowloadImageBlock(error,downloadImg,urlImage,image);
});
}
@end
好的,在此之后我会向你解释我该怎么做:
QUICKly 是一个单例实例,如果有图像,则在特定文件夹中搜索。
是设置图像并完成,没有开始下载然后设置并完成。
我已经看到这种方法对于内存管理非常强大,因为我创建了一个 NSMutabeDictionary,其名称为图像名称,如键,而 UIImage 为值,因此我只对同一图像的多个实例使用一个指针 [然后我在内存中分配图片只是第一次]。
-(void)imageContentUrl:imageURL andImage:(UIImage**)image
如果文件存在,则此方法在目录中搜索并设置图像-> 现在我会像运行 NSError 一样操作,因此我将 UIImage 传递给此方法** 像这样
UIImage* image = nil;
[[GestoreImmagini instance] imageContentUrl:@"URL/Immage/For/Download.jpg" andImage:&image];
[self.imageEvent setImage:image];
我在异步中设置图像。
这是我的想法,但图像已下载并设置(如果我登录,则打印在控制台上)但 self.imageEvent 不显示图像...有人可以帮助我吗?
@Cy-4AH 回答后编辑
正确!!是我的错吗...在 C++ 之后,我有更多的时间在没有指针的情况下工作,我忘记了 somting...
tnx 但现在是下载后代码的第二部分以及从块中返回的图像未设置... [修正 *]
[self downloadImageAtUrl:imageURL andImagePointer:image withHendlerBlock:^(NSError *error, UIImage *image, NSString *urlImage,UIImage** doubleImagePointer) {
if (!error) {
NSLog(@"Stampa Double Pointer IMG -> %@",image);
*doubleImagePointer = image;
[self saveImage:image andName:imageName];
[self.imageDict setValue:image forKey:imageName];
}else{
NSLog(@"Stampa Errore: %@",error);
}
}];
为什么?理论上指针是一样的……
@调试后编辑
我在日志 2.0、2.1 和 2.2 的图像打印 (null) 中发现了问题,这是正确的,因为我没有设置它,但在 2.3 中我遇到了 BAD_ACCESS 崩溃,就像错过所有参考一样。当我在 dowloadImageBlock() 中打印它时也会发生同样的情况。
-(void)downloadImageAtUrl:(NSString*)urlImage andImagePointer:(UIImage**)image withHendlerBlock:(void(^)(NSError* error,UIImage* imageDownloaded,NSString* urlImage,UIImage** doubleImagePointer))dowloadImageBlock{
NSLog(@" 2.0 - ||| ----- -------- -----------> %@",*image);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@" 2.1 - ||| ----- -------- -----------> %@",*image);
dispatch_async(queue, ^{
NSLog(@" 2.2 - ||| ----- -------- -----------> %@",*image);
UIImage* downloadImg = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlImage]]];
NSError* error = nil;
if (downloadImg) {
NSLog(@"Immagine Acquisita");
}else{
NSLog(@"Errore Download Compilazione Errore");
error = [NSError errorWithDomain:DownloadErrorDomain code:0 userInfo:@{DownloadErrorDomain: @"Errore nel download dell'immagine, non si hanno a disposizione ulteriori strumenti per comprenderne la natura. Effettuare un ceck della URL"}];
}
NSLog(@" 2.3 - ||| ----- -------- -----------> %@",*image);
dowloadImageBlock(error,downloadImg,urlImage,image);
});
}
【问题讨论】:
标签: ios objective-c image uiimageview uiimage