【iphone应用开发】iPhone应用开发之四:UIImageView和UIWebView的详细讲解
来源:互联网 发布:mmd软件下载 编辑:程序博客网 时间:2024/06/04 18:26
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 ObjectNSURLRequest *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的利用,这两个都是比较有用的,在以后的开发中布局会经常用到,最后,谢谢大家支持。欢迎大家拍砖。
- 【iphone应用开发】iPhone应用开发之四:UIImageView和UIWebView的详细讲解
- 【iphone应用开发】UIWebView的详细讲解
- 【iphone应用开发】iPhone应用开发之三:UILable和UITextField的详细讲解
- 【iphone应用开发】UITableView的详细讲解
- 【iphone应用开发】UIColor的详细讲解
- 【iphone应用开发】NSArray的详细讲解
- 【iphone应用开发】iphone 应用开发之二:UITextView控件的详细讲解
- 【iphone应用开发】Iphone应用开发之五: UIScrollView的详细讲解
- 【iphone应用开发】iphone应用开发之六:UITableView的详细讲解(一)
- 转载【iphone应用开发】iphone应用开发之六:UITableView的详细讲解(一)
- 【iphone应用开发】iphone应用开发之六:UITableView的详细讲解(一)
- 【iphone应用开发】iphone应用开发之六:UITableView的详细讲解(一)
- Iphone应用开发之五: UIScrollView的详细讲解
- 转载iphone 应用开发之二:UITextView控件的详细讲解
- 转载iphone 应用开发之二:UITextView控件的详细讲解
- iphone应用开发之六:UITableView的详细讲解(一)
- Iphone 开发之 UIWebview
- [iphone游戏开发]iphone-Cocos2D游戏开发之二:精灵表的详细讲解(一)和Zwoptex工具
- uboot与linux之间参数的传递
- java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22)
- 一个java执行exe的小例子
- 计算机研究生院校排名及学科分类
- JS将字符串类型的时间格式转换为date格式
- 【iphone应用开发】iPhone应用开发之四:UIImageView和UIWebView的详细讲解
- Vim配置文件(适用于gVim)
- flier
- 如何用Javascript获取HTML的第二列第三个单元格的内容?
- 直接调用类成员函数地址
- 边缘地带如何选择
- "疯狂的"Chrome
- PostgreSQL代码整体结构(一)
- 我该如何来寻你