UIImageView 和 UIWebView 小結

 

本文轉載至:http://www.cnblogs.com/huangdongcheng/archive/2011/11/21.htmlhtml

1.UIImageView的講解web

(1)初始化緩存

UIImageView  *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,45.0,300,300)];

imageView.image = [UIImage imageNamed:@"a.png"];//加載入圖片

[self.view addSubView:image];

 

 

也能夠這樣聲明:app

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的方法:以下:ide

-》1佈局

 

[UIImage imageNamed:@"a.png"];

 

-》2動畫

NSString *path = [[NSBundle mainBundle] pathForResource:@」icon」
ofType:@」png」];
myImage = [UIImage imageWithContentsOfFile:path];

 

若是找到圖片,裝載到iPhone系統緩存圖象。那意味圖片是(理論上)放在內存裏做爲cache的。所以若是圖片資源多了或大了,此方式容易引發發生內存警告從而致使自動退出的問題。google

最好是經過直接讀取文件路徑[UIImage imageWithContentsOfFile]解決掉這個問題.atom

NSImage *image = [[NSImage alloc]initWithContentsOfURL:(NSURL *)];

NSImage *image = [[NSImage alloc]initWithContentsOfFile:(NSString *)];
 
最後要記得釋放掉image。 

 

(2)利用UIImageView實現幻燈片效果url

 

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

相關文章
相關標籤/搜索