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 Show Image and Display Picture - UIImage (Objective-C,iPhone)

iOS/iPhone Show Image and Display Picture - UIImage (Objective-C,iPhone,iPad) บทความนี้จะเป็นการเขียนโปรแกรมยน iOS แบบง่าย ๆ เกี่ยวกับการแสดงผลรูปภาพ (Image) บน iPhone และ iPad โดยใช้รายการ Object ของ Interface Builder ที่มีชื่อว่า Image View (UIImage) ซึ่งเป็น Object ที่ใช้สำหรับแสดงรูปภาพ Image หรือรายการ Picture ต่าง ๆ ได้ โดยในตัวอย่างนี้จะแสดงรูปภาพที่อยู่ใน Source ของ Project แบบง่าย ๆ ส่วนการแสดงผลรูปภาพจาก Server สามารถอ่านได้จากบทความถัดไป

iOS/iPhone Show Image and Display Picture

Show Image and Display Picture - UIImage


ในการแสดงรูปภาพบน Image View สามารถกำหนด Source ได้จากส่วนของ Design โดยการลาก Object ที่มีชื่อว่า Image View แล้วสามารถกำหนด Image Source ได้ทันที หรือจะกำหนดในส่วนของภาษา Objective-C ด้วย UIImage ตามตัวอย่าง

    UIImage* img = [UIImage imageNamed:@"girl1.jpg"];
    imgView.image = img;




เริ่มต้นการสร้างบน Xcode แสดงผลบน iPhone

iOS/iPhone Show Image and Display Picture

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

iOS/iPhone Show Image and Display Picture

สร้าง Project เลือกตามรูป

iOS/iPhone Show Image and Display Picture

เป็นโครงสร้างของไฟล์ใน Xcode

iOS/iPhone Show Image and Display Picture

ทำการ Copy ไฟล์ รูปภาพไปไว้ในโฟเดอร์ของ Project

iOS/iPhone Show Image and Display Picture

ทำการ Import ไฟล์เข้ามาใน Project โดยการคลิกขวาเลือก Add Files to...

iOS/iPhone Show Image and Display Picture

เลือกไฟล์ใน Project ซึ่งเป็นรูปภาพ

iOS/iPhone Show Image and Display Picture

แสดงไฟล์รูปภาพที่ถูก Import เข้ามา








iOS/iPhone Show Image and Display Picture

กลับมาที่หน้าจอ UI ออกแบบ Interface Builder ดังรูป โดยการลาก Object ที่มีชื่อว่า Image View เข้ามาวางในหน้าจอ

iOS/iPhone Show Image and Display Picture

ในกรณีที่ต้องการแสดงรูปภาพทันที สามารถกำหนด Source ของรูปภาพได้เหมือนในรูปตัวอย่าง ซึ่งรายการรูปภาพใน Project แสดงมาให้เลือก เราไม่ต้องพิมพ์เข้าไปเอง

iOS/iPhone Show Image and Display Picture

เพิ่ม Object ชื่อว่า Button โดยจะทดสอบสร้างปุ่มสำหรับเปลี่ยนรูปภาพ

iOS/iPhone Show Image and Display Picture

ทำการเชื่อม IBOutlet และ IBAction ให้เรียบร้อย

iOS/iPhone Show Image and Display Picture

จะเห็นว่าตอนนี้ใน .m จะมี Method ของ Button สร้างให้อัตโนมัติ

iOS/iPhone Show Image and Display Picture

เราจะสร้างเงื่อนไขการแสดงภาพในแต่ล่ะปุ่ม








Code ของ Objective-C (.h , .m)

ViewController.h
//
//  ViewController.h
//
//  Created by Weerachai on 10/28/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

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

- (IBAction)btnImg1:(id)sender;
- (IBAction)btnImg2:(id)sender;
- (IBAction)btnImg3:(id)sender;
- (IBAction)btnImg4:(id)sender;


@end

ViewController.m
//
//  ViewController.m
//
//  Created by Weerachai on 10/28/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)btnImg1:(id)sender {
    UIImage* img = [UIImage imageNamed:@"girl1.jpg"];
    imgView.image = img;
}

- (IBAction)btnImg2:(id)sender {
    UIImage* img = [UIImage imageNamed:@"girl2.jpg"];
    imgView.image = img;
}

- (IBAction)btnImg3:(id)sender {
    UIImage* img = [UIImage imageNamed:@"girl3.jpg"];
    imgView.image = img;
}

- (IBAction)btnImg4:(id)sender {
    UIImage* img = [UIImage imageNamed:@"girl4.jpg"];
    imgView.image = img;
}

@end


Screenshot

iOS/iPhone Show Image and Display Picture

แสดงรูปภาพ

iOS/iPhone Show Image and Display Picture

คลิกที่ Button เพื่อเปลี่ยนรูปภาพ

iOS/iPhone Show Image and Display Picture

คลิกที่ Button เพื่อเปลี่ยนรูปภาพ


.

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-29 15:15:24 / 2017-03-26 00:20:02
  Download : Download  iOS/iPhone Show Image and Display Picture - UIImage (Objective-C,iPhone)
 Sponsored Links / Related

 
iOS/iPhone Background Colors and Background Image Example (iPhone,iPad)
Rating :

 
iOS/iPhone Set Image Alpha/Opacity with Slider (UIImageView and UISlider)
Rating :

 
iOS/iPhone Page Control and Image View (UIImageView) (Objective-C, iPhone, iPad)
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 01
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 อัตราราคา คลิกที่นี่