iPhone: UIImage load from URL
UIImage load from URL
เก็บตกการโหลดรูปอีกครั้งครับ ตอนนี้หากเราอยากจะโหลดรูปจากอินเตอร์เน็ตเข้ามาแสดงที่ UIImageView เราจะทำอย่างไร? ผมมีตัวอย่างการโหลดภาพจาก URL: https://adayd3sign.wordpress.com/wp-content/uploads/2011/03/image-path-2.png มานำเสนอครับ ..มาลองดูกัน
ขั้นตอน
1. เริ่มด้วยการสร้าง NSURL ด้วย NSString ลิ้งรูปของเรา
NSURL *imageURL = [[NSURL alloc] initWithString:@"https://adayd3sign.wordpress.com/wp-content/uploads/2011/03/image-path-2.png"];
2. สร้าง NSData จากการดึงค่ารูปจาก url
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
3. สร้างUIImage ด้วย NSData จากข้อที่ 2 แล้วก้อเอาไปใส่ในรูปของ UIImageView
image.image = [UIImage imageWithData:imageData];
เสร็จแล้วครับ ง่ายมากๆ
Reference
http://getsetgames.com/2009/12/05/iphonedev-advent-tip-5-how-to-load-a-uiimage-from-a-url/
adaydesign :)
Posted on มีนาคม 15, 2011, in [Dev] iOS and tagged imageWithData, initWithContentOfURL, iphone, load from url, load image from url, NSData, NSURL, UIImage, UIImage load from URL, UIImageView. Bookmark the permalink. ใส่ความเห็น.
ใส่ความเห็น
Comments 0