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 View and NSURLConnection ActivityIndicator Progress

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress บทความนี้จะเป็นการเขียน iOS เพื่อแสดง Image ด้วย Image View ที่อยู่บน URL (Web Server) โดยการใช้ Class ของ NSURLConnection และมีการ Apply ใช้ UIActivityIndicatorView ในการแสดงสถานะกำลังโหลดข้อมูลจาก URL ของเว็บไซต์ และถือได้ว่าเป้นการทบทวนการใช้งาน NSURLConnection และ UIActivityIndicatorView ไปด้วย

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress


เพื่อความเข้าใจเกี่ยวกับ NSURLConnection และ UIActivityIndicatorView ในการเขียนเชื่อมต่อ และการแสดง Progress สถานะการทำงาน แนะนำให้อ่านศึกษา 2 บทความนี้ก่อนครับ

iOS/iPhone NSURLConnection (Objective-C)

iOS/iPhone Activity Indicator View and AlertView (UIAlertView / UIActivityIndicatorView)


Example การแสดง Image View จาก URL ด้วย NSURLConnection และ UIActivityIndicatorView

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress

ทดสอบเรียก URL ผ่าน Web Browser

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress

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

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress

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

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress

ตอนนี้หน่าจอ View จะยังว่าง ๆ

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress

ให้สร้าง Image View บนหน้าจอ View และเชื่อม IBOutlet ให้เรียบร้อย








ViewController.h
//
//  ViewController.h
//  NSURLConnectionApp
//
//  Created by Weerachai on 12/8/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UIImageView *imgView;
    IBOutlet UIActivityIndicatorView *loading;
}

@property(nonatomic,assign) NSMutableData *receivedData;

@end


ViewController.m
//
//  ViewController.m
//  NSURLConnectionApp
//
//  Created by Weerachai on 12/8/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize receivedData;


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    NSURLRequest *theRequest =
    [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.thaicreate.com/url/girl.jpg"]
                     cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                 timeoutInterval:10.0];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    [loading startAnimating];
    
    if (theConnection) {
        self.receivedData = nil;
    } else {
		UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad"  delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
		[connectFailMessage show];
		[connectFailMessage release];
    }
    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    receivedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    sleep(1);
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
    [connection release];
    [receivedData release];
    
    // inform the user
    UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    [didFailWithErrorMessage show];
    [didFailWithErrorMessage release];
	
    //inform the user
    NSLog(@"Connection failed! Error - %@", [error localizedDescription]);
    
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if(receivedData)
    {
        NSLog(@"%@",receivedData);
        
        UIImage *image = [[UIImage alloc] initWithData:receivedData];
        
		//self.image = image;
		[self performSelectorOnMainThread:@selector(setImageView:) withObject:image
                            waitUntilDone:YES];
        
		[image release];
        [loading stopAnimating];
        [loading setHidden:TRUE];
    }
    
                                                 
    // release the connection, and the data object
    [connection release];
    [receivedData release];
}

// Display Image
- (void) setImageView:(UIImage *) img
{
    imgView.image = img;
}

                                                 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [imgView release];
    [loading release];
    [super dealloc];
}
@end









Screenshot

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress

ทดสอบการทำงาน ในณะที่กำลังโหลดข้อมูลด้วย NSURLConnection จะแสดง UIActivityIndicatorView

iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress

หลังจากที่แสดงข้อมูลเรียบร้อยแล้ว UIActivityIndicatorView ก็จะหายไป

   
Share


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


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


   


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

 
iOS/iPhone Activity Indicator View and AlertView (UIAlertView / UIActivityIndicatorView)
Rating :

 
iOS/iPhone Activity Indicator View (ActivityIndicator,UIActivityIndicatorView) Example (iPhone,iPad)
Rating :

 
iOS/iPhone Progress View (UIProgressView) Example (iPhone,iPad)
Rating :

 
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 Image URL Display an Image 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 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 02
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 อัตราราคา คลิกที่นี่