首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

iPhone应用开发之4:UIImageView和UIWebView的详细讲解

2012-09-10 
iPhone应用开发之四:UIImageView和UIWebView的详细讲解From:?http://www.2cto.com/kf/201111/111885.html?

iPhone应用开发之四:UIImageView和UIWebView的详细讲解

From:?http://www.2cto.com/kf/201111/111885.html

?

?

Andy-清风原创,转载请注明,谢谢。

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:[NSURLURLWithString:@"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>?

?

?

?

@interfaceImagesViewController : 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?

?

?

?

@implementationImagesViewController?

?

?

?

@synthesize imageView,slider;?

?

?

?

- (void)dealloc?

?

{?

?

???????? [imageView release];?

?

???????? [slider release];?

?

?

?

???????? [super dealloc];?

?

}?

?

?

?

- (void)viewDidLoad?

?

{????????

?

???????? [super viewDidLoad];?

?

?

?

???????? self.title = NSLocalizedString(@"ImagesTitle",@"");?

?

???????? self.imageView.animationImages = [NSArray arrayWithObjects:?

?

????????? [UIImageimageNamed:@"scene1.jpg"],?

?

????????? [UIImageimageNamed:@"scene2.jpg"],?

?

????????? [UIImageimageNamed:@"scene3.jpg"],?

?

????????? [UIImageimageNamed:@"scene4.jpg"],?

?

????????? [UIImageimageNamed:@"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.slidersetAccessibilityLabel: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 markUIViewController 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 theUIWebView.?

[webView loadRequest:requestObj];?

}?

?

?

并在你的delegate里面载入跟IB一起的XIB文件,如下:

?

-(void)applicationDidFinishLaunching:(UIApplication *)application {?

?

self.wvTutorial =[[WebViewController alloc] initWithNibName:@”WebView” bundle:[NSBundlemainBundle]];?

?

[windowaddSubview:[wvTutorial view]];?

?

// Override point forcustomization after app launch?

[window makeKeyAndVisible];?

}?

?

?

今天就讲UIImageView和UIWebView的利用,这两个都是比较有用的,在以后的开发中布局会经常用到,最后,谢谢大家支持。欢迎大家拍砖。

热点排行