IPhone application development 4: detailed explanation of UIImageView and UIWebView

Source: Internet
Author: User

 

Andy-original breeze. For more information, see. Thank you.

1. Explanation of UIImageView

(1) initialization

 

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

 

ImageView. image = [UIImage imageNamed: @ "a.png"]; // load the image

 

[Self. view addSubView: image];

 

 

You can also declare it as follows:

 

UIImage * image = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL: [NSURLURLWithString: @ "http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"];

UIImageView * imageView = [[UIImageView alloc] initWithImage: image];

 

There are two ways to load a UIImage when adding an image:

-"1

 

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

 

-"2

 

NSString * path = [[NSBundle mainBundle] pathForResource: @ "icon"

OfType: @ "png"];

MyImage = [UIImage imageWithContentsOfFile: path];

 

If you find the image, load it to the iPhone system to cache the image. That means that the image is stored in the memory as a cache (theoretically. Therefore, if the image resources are too large or too large, this method may cause a memory warning and cause the automatic exit problem.

It is best to solve this problem by Directly Reading the file path [UIImage imageWithContentsOfFile.

 

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

NSImage * image = [[NSImage alloc] initWithContentsOfFile :( NSString *)];

 

 

Remember to release the image.

 

 

(2) Use UIImageView to achieve the slide Effect

 

Use UIImageView and UISider to create slides.

 

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

 

 

 

 

First, load multiple images to the animationImages of the imageView. Set the animation interval animationDuration, and set UISider to control the speed at which slides are played, so that images can be played as slides.

 

2. Explanation of UIWebView

(1) initialization

Declare UIWebView in the. h file.

 

@ Interface WebViewController: UIViewController {

 

IBOutlet UIWebView * webView;

}

 

@ Property (nonatomic, retain) UIWebView * webView;

 

@ End

 

 

Initialize UIWebView in the. m file and load the URL to be read as follows:

 

-(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];

}

 

 

And load the XIB file with IB in your delegate, as follows:

 

-(Void) applicationDidFinishLaunching :( UIApplication *) application {

 

Self. wvTutorial = [[WebViewController alloc] initWithNibName: @ "WebView" bundle: [NSBundlemainBundle];

 

[WindowaddSubview: [wvTutorial view];

 

// Override point forcustomization after app launch

[Window makeKeyAndVisible];

}

 

 

Today we will talk about the use of UIImageView and UIWebView. Both of them are useful and will be used frequently in future development. Finally, thank you for your support. You are welcome to make a brick.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.