【iphone应用开发】iPhone应用开发之四:UIImageView和UIWebView的详细讲解
Posted on 2011-11-21 17:30 Andy--清风 阅读(7096) 评论(1) 编辑 收藏 举报1.UIImageView的讲解
(1)初始化
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,45.0,300,300)]; imageView.image = [UIImage imageNamed:@"a.png"];//加载入图片 [self.view addSubView:image];
也可以这样声明:
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
在加载入图片的时候有两种加载UIImage的方法:如下:
-》1
[UIImage imageNamed:@"a.png"];
-》2
NSString *path = [[NSBundle mainBundle] pathForResource:@”icon” ofType:@”png”]; myImage = [UIImage imageWithContentsOfFile:path];
如果找到图片,装载到iPhone系统缓存图象。那意味图片是(理论上)放在内存里作为cache的。因此如果图片资源多了或大了,此方式容易引起发生内存警告从而导致自动退出的问题。
最好是通过直接读取文件路径[UIImage imageWithContentsOfFile]解决掉这个问题.
NSImage *image = [[NSImage alloc]initWithContentsOfURL:(NSURL *)]; NSImage *image = [[NSImage alloc]initWithContentsOfFile:(NSString *)];
最后要记得释放掉image。
(2)利用UIImageView实现幻灯片效果
利用UIImageView和UISider来制作幻灯片。
ImagesViewController.h #import <UIKit/UIKit.h> @interface ImagesViewController : UIViewController { UIImageView *imageView; UISlider *slider; } @property (nonatomic, retain) IBOutlet UIImageView *imageView; @property (nonatomic, retain) IBOutlet UISlider *slider; - (IBAction)sliderAction:(id)sender; @end
ImagesViewController.m
#import "ImagesViewController.h"
#import "Constants.h"
#define kMinDuration 0.0
#define kMaxDuration 10.0
@implementation ImagesViewController
@synthesize imageView, slider;
- (void)dealloc
{
[imageView release];
[slider release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"ImagesTitle", @"");
self.imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"scene1.jpg"],
[UIImage imageNamed:@"scene2.jpg"],
[UIImage imageNamed:@"scene3.jpg"],
[UIImage imageNamed:@"scene4.jpg"],
[UIImage imageNamed:@"scene5.jpg"],nil];
imageView.animationDuration = 5.0;
[self.imageView stopAnimating];
imageView.image = [UIImage imageNamed:@"a.png"];
[self.imageView setIsAccessibilityElement:YES];
[self.imageView setAccessibilityLabel:self.title];
[self.slider setAccessibilityLabel:NSLocalizedString(@"DurationSlider",@"")];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.imageView = nil;
self.slider = nil;
}
- (IBAction)sliderAction:(id)sender
{
UISlider* durationSlider = sender;
self.imageView.animationDuration = [durationSlider value];
if (!self.imageView.isAnimating)
[self.imageView startAnimating];
}
#pragma mark -
#pragma mark UIViewController delegate methods
- (void)viewWillDisappear:(BOOL)animated
{
[self.imageView stopAnimating];
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)viewWillAppear:(BOOL)animated
{
[self.imageView startAnimating];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
}
@end
首先通过加载多张图片进入imageView的animationImages里面。再设置动画的时间间隔animationDuration,并设置UISider控制幻灯片播放的速度,让图片像幻灯片那么播放。
2.UIWebView的讲解
(1)初始化
在.h文件中声明UIWebView。
@interface WebViewController : UIViewController { IBOutlet UIWebView *webView; } @property (nonatomic, retain) UIWebView *webView; @end
在.m文件中初始化UIWebView并载入要读取的URL,如下:
- (void)viewDidLoad { NSString *urlAddress = @”http://www.google.com”; //Create a URL object. NSURL *url = [NSURL URLWithString:urlAddress]; //URL Requst Object NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; //Load the request in the UIWebView. [webView loadRequest:requestObj]; }
并在你的delegate里面载入跟IB一起的XIB文件,如下:
- (void)applicationDidFinishLaunching:(UIApplication *)application { self.wvTutorial = [[WebViewController alloc] initWithNibName:@”WebView” bundle:[NSBundle mainBundle]]; [window addSubview:[wvTutorial view]]; // Override point for customization after app launch [window makeKeyAndVisible]; }
今天就讲UIImageView和UIWebView的利用,这两个都是比较有用的,在以后的开发中布局会经常用到,最后,谢谢大家支持。欢迎大家拍砖。
共同的事业,共同的斗争,可以使人们产生忍受一切的力量。 —— 奥斯特洛夫斯基