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 :)

About adaydesign

นักพัฒนาแอพพลิเคชั่นบนโมบาย [iPhone Android] คนหนึ่งซึ่งอยากจะบันทึกเรื่องราวของเส้นทางนักพัฒนาโมบายแอพพลิเคชั่นเอาไว้ในบล็อคน้อยๆ แห่งนี้ เผื่อจะแบ่งบันและแลกเปลี่ยนความรู้ให้กับคนอื่นๆ ได้บ้าง :) Mobile Application Developer; Flash Mobile, iPhone/iPad, Android Mobile Phone/Tablet and Blackberry Playbook.

Posted on มีนาคม 15, 2011, in [Dev] iOS and tagged , , , , , , , , , . Bookmark the permalink. ใส่ความเห็น.

ใส่ความเห็น