如何使程序加载Web图像并在自定义视图上显示?

时间:2011-01-22 20:24:26

标签: image nsview nsurl dealloc

好吧,我正在学习如何在Mac上编程,而我却厌倦了寻找答案,你能解释一下我在程序中做错了什么。

首先,我在窗口上拖动了2个按钮(加载图像和卸载)。然后,我拖动了一个自定义视图项,这就是我对Test_Loading_ImagesAppDelegate的所作所为。 h

#import <Cocoa/Cocoa.h>

@interface Test_Loading_ImagesAppDelegate : NSView {
    NSWindow *window;
    IBOutlet NSView *mypicture;
}

- (IBAction)LoadImage: (id)sender;
- (IBAction)Unload: (id)sender;


@property (assign) IBOutlet NSWindow *window;

@end

之后,我通常在Interface Builder上链接了一些内容并制作了Test_Loading_ImagesAppDelegate。 m

#import "Test_Loading_ImagesAppDelegate.h"

@implementation Test_Loading_ImagesAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application 
}
-(IBAction)LoadImage: (id) sender {
    NSURL *myurl;
    NSImage *image;
    NSData *myurldata;
    myurl = [NSURL URLWithString:@"http://....jpg"];
    myurldata = [NSData dataWithContentsOfURL:myurl];
    image = [NSImage initWithData:myurldata];
    [mypicture setImage: image];

}

-(IBAction)Unload: (id) sender {

//Well, still thinking how I'm going to do this, and I would like to make another question: If I dealloc the View or something else the image will disappear?

}

@end

提前致谢。

1 个答案:

答案 0 :(得分:0)

嗯,再次来到这里之后我发现这个http://www.markj.net/iphone-asynchronous-table-image/适用于iPhone,但可以很容易地适应OS X.感谢。