我似乎有一个指针问题,我似乎无法解决它。有人可以帮帮我吗?
-(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]);
}
Target
是UIImage
类型的私有变量(声明:UIImage *Target;
)
CompiledImage
也是UIImage
。我想要做的是将目标地址的内容设置为CompiledTarget的内容。这会导致以下错误:
从不兼容类型'UIImage * __ strong'
分配给'UIImage'
我试过了:
memccpy(__bridge Target, &CompiledImage, sizeof(Target),sizeof(Target));
我收到此错误:
预期表达
答案 0 :(得分:5)
您需要将其设置为,
Target = CompiledImage;
不需要*
。由于两者都是指针,因此如果使用上面的代码,基本上就是分配内存地址而不是复制内容。
在旁注中,请使用小写字母开始变量名称。 Target
通常代表一个类名。根据苹果编码惯例,它应该是target
。
根据您的评论,您可以执行以下操作,
在ViewController类中,将UIImage
声明为@property
,
@property (nonatomic, retain) UIImage *downloadedImage;
在进行网址调用时,
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]);
}
<强> UPDATE2:强>
使用传递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,让委托方法在下载图像后设置图像。
答案 1 :(得分:1)
让目标成为pTarget?指向指针然后它是有道理的
*pTarget = compiledImage
(因为它经常用NSErrors完成......例如在核心数据中)
你不能记住这些东西,因为目标没有在堆上分配正确的大小。
你不能复制内存中的真实UIImage字节,因为你并不了解它和实现细节。你甚至不知道它有多大!
但是你可以将指针复制到图像 - 它的内存地址
UIImage *pointer1 = [UIImage imageWithFoo];
UUImage *pointer2 = pointer1;
现在你也可以声明一个空指针并稍后填写
UIImage *target = nil;
UIImage *pointer1 = [UIImage imageWithFoo];
target = pointer1;
到目前为止一直很好,但是如果你想通过一个函数进行操作并且应该填充它呢?
target = [self calculateImage];
但是现在你有异步功能
[self asyncCalculateImage:&target]; // you pass it a POINTER to the POINTER to fill :)
...
- (void)asyncCalculateImage:(UIImage**)pTarget {
UIImage *pointer1 = [UIImage imageWithFoo];
*pTarget = pointer1; //DEREFERENCE and fill pTarget
}
答案 2 :(得分:0)
为什么你再次放*,这里CompiledImage本身就是一个指针。
只需使用Target=CompiledImage;