开发者

How to make a program load an web image and show it on a Custom View?

开发者 https://www.devze.com 2023-02-05 23:49 出处:网络
Well, I\'m learning how to program on a mac, and I\'m tired of searching the for answers, can you guys explain me what I did wrong on my program.

Well, I'm learning how to program on a mac, and I'm tired of searching the for answers, can you guys explain me what I did wrong on my program.

First, I dragged 2 buttons on my window (Load Image, and Unload). Then, I dragged a custom view item and here`s w开发者_StackOverflowhat I did with the 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

After that, I linked up things on Interface Builder normally and made the 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

Thanks in advance.


Well, after coming here again I've found this http://www.markj.net/iphone-asynchronous-table-image/ it's for iPhone but can be adapted to OS X easily. Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消