Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

iOS/iPhone Image URL Display an Image from URL (Website)

iOS/iPhone Image URL Display an Image from URL (Website) ในการเขียน iOS App ต่าง ๆ เช่น App ที่เกี่ยวกับ ข่าว (News) ซึ่งจะมีการอัพเดดข่าวต่าง ๆ ข้อมูลเหล่านี้นั้นล้วนถูกจัดเก็บไว้ที่ Web Server และ App มีหน้าที่แค่เรียกข้อมูลมาแสดงเท่านั้น โดยปกติแล้วข้อมูลพวกข้อความต่าง ๆ จะสามารถส่งได้ในรูปแบบของ JSON เพราะสามารถส่งข้อมูลที่เป็นชุดของข้อมูลที่สามารถนำไปถอดรหัสข้อความและแสดงผลตามตำแหน่งต่าง ๆ ได้ตามต้องการ แต่รูปภาพ (Image/Picture) นั้นสามารถส่งได้แค่ URL ที่อยู่ของรูปภาพเท่านั้น และเราจะใช้การอ่านรูปภาพด้วย dataWithContentsOfURL ซึ่งจะแปลงรูปภาพให้เป็น Data และค่อยนำ Data นั้นไปแปลงให้อยู่ในรูปแบบที่สามารถแสดงผลบน Image View ได้ (UIImageView)

iOS/iPhone Image URL Display an Image from URL (Website)

iOS/iPhone Image URL Display an Image from URL (Website)


iOS/iPhone and JSON (Create and JSON Parsing)


ในบทความนี้เราจะมาเรียนรู้การอ่านและแสดงรูปภาพที่อยู่บน Web Server และใช้การแสดงผลบนหน้าจอ View แบบง่าย ๆ ก่อนอื่นเรามาดูรูปแบบการอ่าน URL ของรูปภาพแบบง่าย ๆ

     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;


เป็นการแสดงรูปภาพจาก URL ของ https://www.thaicreate.com/url/girl.jpg

iOS/iPhone Image URL Display an Image from URL (Website)

รูปภาพเมื่อแสดงผลบน Web Browser

iOS/iPhone Image URL Display an Image from URL (Website)

เริ่มต้นด้วยการสร้าง Application แบบง่าย ๆ ด้วย Single View Application








iOS/iPhone Image URL Display an Image from URL (Website)

เลือกและไม่เลือกรายการดังรูป

iOS/iPhone Image URL Display an Image from URL (Website)

ตอนนี้หน้าจอ View ของเรายังว่าง ๆ ไม่มีรายการ Object ใด ๆ ทั้งสิ้น

iOS/iPhone Image URL Display an Image from URL (Website)

สร้าง Image View และ Button บนหน้าจอ View

iOS/iPhone Image URL Display an Image from URL (Website)

ใน Class ของ .h ให้สร้าง IBOutlet และ IBAction ดังรูป

iOS/iPhone Image URL Display an Image from URL (Website)

เมื่อกลับมาใน .m จะมี method ที่ถูกสร้างขึ้นดังรูป

iOS/iPhone Image URL Display an Image from URL (Website)

ใส Class ของ.m ใส่ Code ง่าย ๆ ดังนี้

- (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;
 
}


Screenshot

iOS/iPhone Image URL Display an Image from URL (Website)

แสดงหน้าจอ App หน้าแรก

iOS/iPhone Image URL Display an Image from URL (Website)

คลิกที่ Button ก็จะแสดงรูปภาพบน Image View ที่อยู่บน URL (Webiste) ของ Web Server

Code ทั้งหมดของ .h และ .m ในภาษา Objective-C

ViewController.h
//
//  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
//
//  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









iOS/iPhone and JSON (Create and JSON Parsing)

iOS/iPhone Get the String contents from URL (Website)

iOS/iPhone Table View and JSON (UITableView from JSON Parser)


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-12-03 09:29:21 / 2017-03-26 09:07:29
  Download : Download  iOS/iPhone Image URL Display an Image from URL (Website)
 Sponsored Links / Related

 
iOS/iPhone Web View (UIWebView) Open Web Site and HTML (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Get the String contents from URL (Website)
Rating :

 
iOS/iPhone Display Image on Table View from JSON URL (Web Site)
Rating :

 
iOS/iPhone NSURLConnection (Objective-C)
Rating :

 
iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress
Rating :

 
iOS/iPhone NSURLRequest Example (Objective-C)
Rating :

 
iOS/iPhone NSURLConnection POST Method and Send Parameter (Objective-C)
Rating :

 
iOS/iPhone NSMutableURLRequest Example (Objective-C)
Rating :

 
iOS/iPhone NSURLConnection and PHP MySQL / JSON (TableView,UITableView)
Rating :

 
iOS/iPhone NSURLConnection Show Progress and Activity Indicator View (UIActivityIndicatorView)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่