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的利用,這兩個都是比較有用的,在以後的開發中布局會經常用到,最後,謝謝大家支持。歡迎大家拍磚。