iphone - iOS 指针问题

标签 iphone objective-c ios xcode pointers

我似乎有一个指针问题,而且我似乎无法修复它。有人可以帮帮我吗?

-(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]);
}

TargetUIImage 类型的私有(private)变量(声明:UIImage *Target;) CompiledImage 也是一个 UIImage。我要做的是将目标地址的内容设置为 CompiledTarget 的内容。这会导致以下错误:

Assigning to 'UIImage' from incompatible type 'UIImage *__strong'

我试过了:

memccpy(__bridge Target, &CompiledImage, sizeof(Target),sizeof(Target));

我得到这个错误:

Expected expression

最佳答案

你只需要将它设置为,

Target = CompiledImage;

不需要*。由于两者都是指针,如果您使用上述代码,基本上您是在分配内存地址而不是复制内容。

附带说明,请以小写字母开头变量名。 Target 通常表示一个类名。根据 Apple 编码约定,它应该是 target

根据您的意见,您可以执行以下操作,

在ViewController类中,声明一个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 中的相同,具有指向相同位置的相同内存地址。

另一种方法是在 NSImageLoader 类中将 UIImage *Target 声明为 UIImage **Target。在调用 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 并让委托(delegate)方法在您下载图像后在其中设置图像。

关于iphone - iOS 指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13787225/

相关文章:

iphone - Titanium appcelerator 是否值得在 ipad、iphone 和 android 上开发基于相机的应用程序?

iphone - 如何创建指向导航栏的指针并添加 UIPanGestureRecognizer?

ios - 奇怪的崩溃 : NSRangeException with "unhelpful" stacktrace

objective-c - 如何在cocoa中选择文件

iphone - Objective C 中的多个字符串替换

iphone - 如何仅检测是否是 iOS 设备并重定向?

objective-c - 如何使 NSTextField 适合包含的 NSMutableAttributedString

ios - APNs 生产 iOS 和 p12 文件

ios - iPhone游戏的CPU使用率..这是一个好的值吗?

ios - 使用 overCurrentContext 呈现 View Controller ,然后推送到导航堆栈会导致导航栏重叠