NSURL *url = [NSURL URLWithString:@"https://www.thaicreate.com/url/girl.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; imgView.image = img;
- (IBAction)btnShowImage:(id)sender { NSURL *url = [NSURL URLWithString:@"https://www.thaicreate.com/url/girl.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; CGSize size = img.size; imgView.image = img; }
// // ViewController.h // imageFromURL // // Created by Weerachai on 12/2/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UIImageView *imgView; } - (IBAction)btnShowImage:(id)sender; @end
// // ViewController.m // imageFromURL // // Created by Weerachai on 12/2/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [imgView release]; [super dealloc]; } - (IBAction)btnShowImage:(id)sender { /* NSURL *url = [NSURL URLWithString:@"https://www.thaicreate.com/url/girl.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; CGSize size = img.size; imgView.image = img; */ NSData *receivedData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://www.thaicreate.com/url/girl.jpg"]] retain]; UIImage *image = [[UIImage alloc] initWithData:receivedData] ; imgView.image = image; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท