您只需将其设置为,
Target = CompiledImage;
不需要*
。由于两者都是指针,因此如果您使用上述代码,您基本上是在分配内存地址而不是复制内容。
附带说明,请以小写字母开头的变量名。Target
通常代表一个类名。根据苹果编码约定,它应该是target
.
根据您的评论,您可以执行以下操作,
在 ViewController 类中,将 a 声明UIImage
为@property
,
@property (nonatomic, retain) UIImage *downloadedImage;
在进行 URL 调用时,
NSImageLoader *imageLoader = [[NSImageLoader alloc] init];
[imageLoader setTarget:self];//setting current viewController as target instead of UIImage
下载图片时,
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Finished Downloading Image: %@" ,[connection.originalRequest.URL absoluteString]);
UIImage *CompiledImage=[UIImage imageWithData:ImageData];
SEL selector=@selector(ImageDownloadingCompleted:Image:);
if([[self Delegate] respondsToSelector:selector]){
[[self Delegate] ImageDownloadingCompleted:self Image:CompiledImage];
}
else{
if(Target){
Target.downloadedImage = CompiledImage;//or [Target setDownloadedImage:CompiledImage];
}
}
// NSLog(@"Image Size:%i", [ImageData length]);
}
在您的 ViewController 类中,现在您可以访问图像,self.downloadedImage
它CompiledImage
与指向相同位置的相同内存地址相同。
另一种方法是UIImage *Target
在UIImage **Target
你的NSImageLoader
类中声明。在调用setTarget
方法时,使用[imageLoader setTarget:&Target];
. 在此方法中,您需要将目标设置为Target = Target;
更新:
根据您的评论,它应该是这样的,
for( NSDictionary *CurrentActivity in [Profile UserActivities]) {
...
UIImage *WineImage = [UIImage imageNamed:@"loader.gif"];
NSImageLoader *loader=[[NSImageLoader alloc] initWithURLString:[NSString stringWithFormat:@"%@%@",[TempSettings URL],[CurrentActivity objectForKey:@"ImageURL"]]];
[loader setTarget:&WineImage];
[loader startDownloading];
[self addSubview:Activity];
Counter++;
}
然后在 NSImageLoader.h 文件@interface 中,
__strong UIImage **Target; //This should be strong not autoreleasing
在 NSImageLoader.m 文件中,
- (void)setTarget:(UIImage *__strong *)iTarget{ //change here also
Target = target;
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Finished Downloading Image: %@" ,[connection.originalRequest.URL absoluteString]);
UIImage *CompiledImage=[UIImage imageWithData:ImageData];
SEL selector=@selector(ImageDownloadingCompleted:Image:);
if([[self Delegate] respondsToSelector:selector]){
[[self Delegate] ImageDownloadingCompleted:self Image:CompiledImage];
}
else{
if(Target){
*Target = CompiledImage;
}
}
// NSLog(@"Image Size:%i", [ImageData length]);
}
更新2:
使用传递 UIImageView 的方法,您可以执行以下操作,
for( NSDictionary *CurrentActivity in [Profile UserActivities]) {
...
UIImage *WineImage = [UIImage imageNamed:@"loader.gif"];
NSImageLoader *loader=[[NSImageLoader alloc] initWithURLString:[NSString stringWithFormat:@"%@%@",[TempSettings URL],[CurrentActivity objectForKey:@"ImageURL"]]];
[loader setTarget:Activity];//pass imageview and let the delegate method set image
[loader startDownloading];
[self addSubview:Activity];
Counter++;
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Finished Downloading Image: %@" ,[connection.originalRequest.URL absoluteString]);
UIImage *CompiledImage=[UIImage imageWithData:ImageData];
SEL selector=@selector(ImageDownloadingCompleted:Image:);
if([[self Delegate] respondsToSelector:selector]){
[[self Delegate] ImageDownloadingCompleted:self Image:CompiledImage];
}
else{
if(Target){
Target.image = CompiledImage;
}
}
// NSLog(@"Image Size:%i", [ImageData length]);
}
在这里传递 imageview 并让委托方法在您下载图像后设置图像。