Iphone and UIImageView display different processing methods of images in different ways iPhone development image processing key points

Source: Internet
Author: User

I am in the beginner stage. I have a lot of good information shared by my predecessors on the Internet. I have found these two articles, which are described as follows:

1) iPhone development of image processing related points http://www.cnblogs.com/lovecode/archive/2011/11/27/2265275.html#commentform, about 4 ways to load pictures;

BelowReprintedThis is the original text about image processing in iPhone development:

IPhone images are usually stored in the following four places:

  • Album(PhotoAlums): You can use the Interactive dialog box provided by the UIImagePickerController class to obtain images from this album.
  • Application Package: Stores images with executable programs, Info. plist files, and other resources. You can use the imageNamed: Method to read these package-based images through the local file path.
  • Sandbox: Using sandbox, you can store files in the Documents, Library, and tmp folders.
  • Internet(Internet): applications can download images from the Internet through URL resources.

 The location of the image file determines how the file is read.. Images and their paths in the album cannot be directly accessed from the application. Only end users can browse and select images to make the selected images available to the application. Images cannot be directly initialized by URLs. Different image source reading methods are also different:

1. load images from application packages

The UIImage class provides a simple way to load any image stored in the application package, that is, call imageNamed through the file name and its extension: method.

myImage = [UIImage imageNamed:@"icon.png"];

To avoid the problem of local image caching and its effective use of memory space, imageWithContentsOfFile can also be used: replace. This method returns the image loaded from a specific path, this path must be provided as a parameter. Of course, to obtain the image path from the application package, you can query the NSBundle class to find the path for the given resource. Sample Code:

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

2. load images from sandbox

By default, each sandbox contains three folders: Documents, Library, and tmp. Images and other data generated by applications are usually located in the Documents folder. In iPhone development, you can call the utility main directory function to reliably locate the top-level sandbox folder. Using NSHomeDirectory () to return results, we can navigate down to the Documents folder to ensure the correct position. Sample Code:

NSString * documentsFolder ()
{
Return [NSHomeDirectory ()
StringByAppendingPathComponent: @ "Documents"];
}
// Load the image
Path = [documentsFolder () stringByAppendingPathComponent: @ "image.png"];
Return [UIImage imageWithContentsOfFile: path];

3. load images from URL Resources

The UIImage class can load images from NSData instances, but it cannot load images directly from URL strings or NSURL objects. Therefore, you can only provide UIImage with data that has been downloaded from the URL (that is, to create an NSData instance initialized by the URL content ).

NSURL * url = [NSURL URLWithString: @ "http://www.cnblogs.com/lovecode/images/demo.jpg"];
UIImage * img = [UIImage imageWithData: [NSData dataWithContentsOfURL: url];

// Similarly, we can directly create class methods
// A specific URL string returns the UIImage constructed by the Resource
+ (UIImage *) imageFromURLString: (NSString *) urlstring
{
Return [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: urlstring];
}

4. load data from the album

The UIImagePickerController class helps us select images from the iPhone album. It provides an independent view controller that presents a view in a modal form. The delegated message sent from the controller can reflect the image selected by the user.

  • Uiimagepickercontrollersourcetypephotolibrary all images synchronized to the iPhone and any Album including the images taken by the user.
  • Uiimagepickercontrollersourcetypesavedphotosalbum only includes album.
  • Uiimagepickercontrollersourcetypecamera allows users to take photos using the iPhone's built-in camera.

The image picker delegate must comply with two Protocols: UINavigationControllerDelegate and UIImagePickerControllerDelegate. In the interface, you must declare the two Protocols for the objects set as the picker delegate.

2) use UIImageView to show the picture http://blog.csdn.net/jasonblog/article/details/7352483 from the network, describes how to use UIImageView to show pictures from the network.

The image downloaded from the Internet is also self. imageView. image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: urlstring];

It also describes the problem that the UI thread will be blocked during image download. The NSOperationQueue method is used to asynchronously load images (NSThread can also be used to asynchronously load images ).

Next, we also take into account the cached images that have been downloaded (that is, before each download, we first determine whether there is a cache, which can optimize the experience and performance ). It is relatively simple to save to the memory. You only need to use NSDictionary for maintenance. Storage to the disk involves reading and writing local files. For details, refer to "file and Data Management ".

For more information, see the original document.

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.