[iOS开发站在巨人肩膀上]之iPhone Images from URL using XML File
来源:互联网 发布:淘宝信用等级快速提升 编辑:程序博客网 时间:2024/05/03 18:39
本文来源:http://www.codeproject.com/KB/iPhone/IPhone_URL_Images.aspx
Introduction
This article is about displaying images on iPhone from web using XML file as medium. In this article, we learn two things. So let’s deal with this article in two phases:
- Parsing XML File in iPhone
- Display images from Web URL
Parsing XML Files in iPhone (NSXML Library)
Apple has provided NSXMLParser
for parsing XML files in iPhone project. This parser takes a web URL as input path (file should reside in the web) and parses the document. Delegate methods of the NSXMLParser
do the remaining work for you.
The below three delegate methods of the NSXMLParser
class are used to parse the XML files.
didStartElement
didEndElement
foundCharacters
didStartElement
This method is sent by parser object to its delegate when it sees the starting tag of the given element. The syntax is as follows:
- (void)parser:(NSXMLParser*)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
didEndElement
This method is sent by parser object to its delegate when it sees the end tag of the given element. The syntax is as follows:
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString *)qName
foundCharacters
This method is sent by parser object to its delegate when it finds the characters between start tag and end tag of the given element. The syntax is as follows:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
UIImage from URL
Displaying an image from the URL is a 3 step process:
- Create an
NSURL
Object from web URL - Load the
NSURL
object with image data inNSData
Object - Assign the image data
NSData
to theUIImage
NSURL *url = [NSURL URLWithString:@”www.ArticleDean.com\images\sample.jpg”];NSData *data = [NSData dataWithContentsOfURL:url];UIImage *image = [[UIImage alloc] intiWithData:data];
This is the minimum knowledge we need before we are going to the original application. Let’s start coding our application.
Step 1: Create a view based application, and name the project as XMLWebImages
.
Step 2: Open XMLWebImagesViewController.h, add IBOutlet
type object scrollView
of UIScrollView
. Synthesize the view object scrollView
in XMLWebImagesViewController.m.
Step 3: Create a new class inherited from UIViewSubclass
and name it as XMLWebView
and add aUIImageView
object as IBOutlet
and call it imageView
. Call getters and setters and synthesize imageView
Object.
#import <UIKit/UIKit.h> @interface XMLWebImageViewController : UIViewController { IBOutlet UIScrollView *webScrollView; }@property (nonatomic, retain) IBOutlet UIScrollView * webScrollView;@synthesize scrollView;
Step 3: Create a new class inherited from UIViewSubclass
and name it as XMLWebView
and add aUIImageView
object as IBOutlet
and call it imageView
. Call getters and setters and synthesize imageView
Object.
@interface XMLWebView : UIView { IBOutlet UIImageView *imageView;}@property (nonatomic, retain) IBOutlet UIImageView *imageView;@synthesize imageView;
Step 4: Create a .XIB interface builder file and connect the imageView
object to the XMLWebView
in the document window to show our images.
Step 5: Create an XMLWebElement
class inherited from NSObject
to hold the UIImage
object to hold the web images.
@interface XMLWebElement: NSObject { UIImage *imgXML;}@property (nonatomic, retain) UIImage * imgXML;@end@synthesize image;
Step 6: Now the time has come to start the parsing and take the images from XML residing in articledean.com. OpenXMLWebImagesViewController.h file and create an object of NSXMLParser
, and collections of XML items into an array of XMLWebElements
and a temp XMLWebElement
object.
@interface XMLWebImagesViewController: UIViewController { IBOutlet UIScrollView *scrollview; NSXMLParser *xmlParser; NSMutableString *currentNode; NSMutableArray *xmlElements; XMLWebElement *tempElement;}
Write setters and getters for this and synthesize these objects in XMLWebImagesViewController.m file.
Now in viewDidLoad
method, allocate memory for xmlElements
and assign the parser with the web XML file.
- (void)viewDidLoad{ [super viewDidLoad]; xmlElements = [[NSMutableArray alloc] init]; xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.articledean.com/images.xml"]]; [xmlParser setDelegate:self]; [xmlParser parse];}
Now it's time to add delegate methods of NSXMLParser
and I am introducing a sample XML file structure here.
<WebImages> <image> <URL>http://www.articledean.com/testimage1.jpg</URL> </image> <images> <URL>http://www.articledean.com/testimage2.jpg</URL> </images></WebImages>
Now fill the delegate methods to retrieve the image URL from web and load the XMLWebElements
array.
- (void)xmlParser:(NSXMLParser *)xmlParser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ if(![elementName compare:@"PictureInfo"]) { tempElement = [[iCodeBlogXMLElement alloc] init]; } else if(![elementName compare:@"imageURL"]) { currentAttribute = [NSMutableString string]; } else if(![elementName compare:@"imageTitle"]) { currentAttribute = [NSMutableString string]; }}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if(![elementName compare:@"PictureInfo"]) { [xmlElementObjects addObject:tempElement]; } else if(![elementName compare:@"imageURL"]) { NSURL *imageURL = [NSURL URLWithString:currentAttribute]; NSData *data = [NSData dataWithContentsOfURL:imageURL]; UIImage *image = [[UIImage alloc] initWithData:data]; [tempElement setImage:image]; } else if(![elementName compare:@"imageTitle"]) { NSLog(@"The image title is %@", currentAttribute); [tempElement setImageTitle:currentAttribute]; } else if(![elementName compare:@"Pictures"]) { [self layoutSubview]; }}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ if(self.currentAttribute) { [self.currentAttribute appendString:string]; }}
We are almost done with the code. Remaining part is a cake walk. Now the arrays are filled with the images URLs. Traverse though the array and assign the images to UIView
and display them in the scroll view.
References
- http://www.ArticleDean.com
- http://icodeblog.com
- [iOS开发站在巨人肩膀上]之iPhone Images from URL using XML File
- [iOS开发站在巨人肩膀上]之 iOS 4 in Action - Using Location Monitoring Service
- [iOS开发站在巨人肩膀上]之How to avoid memory leaks in iPhone applications
- [iOS开发站在巨人肩膀上]之Introduction to iOS Graphics APIs: Part 1
- [iOS开发站在巨人肩膀上]之Reusable collapsable table view for iOS
- [iOS开发站在巨人肩膀上]之How to implement Queue in Objective-C
- [iOS开发站在巨人肩膀上]之How to implement a stack in Objective-C
- [ios 站在巨人的肩膀上开发 之]ASIHttpRequest 使用指南(一)
- [ios 站在巨人的肩膀上开发 之]ASIHttpRequest 使用指南(二)
- 站在巨人的肩膀上做软件开发
- 站在巨人的肩膀上学习Android开发
- 站在巨人的肩膀上学习Android开发
- 站在巨人的肩膀上学习Android开发
- android开发利器--站在巨人肩膀上前行
- 站在巨人的肩膀上学习Android开发
- 站在巨人的肩膀上之软件帮助文档
- 站在巨人的肩膀上,iFunk起航之路
- 站在巨人的肩膀上之bill分享有感
- WPF中如何将流写入Image控件
- 腾讯面试经历
- 原创Linux+apache+mysql+python+mod_python+Django
- 检查数组越界等
- 操作系统面试题
- [iOS开发站在巨人肩膀上]之iPhone Images from URL using XML File
- 二叉树深度优先遍历的非递归算法
- 什么是Shader
- 趁断网之前
- 介绍几个功能强大WINDOWS命令
- [iOS开发站在巨人肩膀上]之How to avoid memory leaks in iPhone applications
- 多线程编程之三——线程间通讯
- davlik虚拟机内存管理之一——内存分配
- [iOS开发站在巨人肩膀上]之How to implement Queue in Objective-C