Reading Image Data
1. UIImage Convenience Methods
myImage = [UIImage imageNamed:@"icon.png"];
2. Finding Images in the Sandbox
NSArray *paths = [NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
return [paths lastObject];
NSString *documentsFolder()
{
return [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
}
path = [documentsFolder() stringByAppendingPathComponent:@"image.png"];
return [UIImage imageWithContentsOfFile:path];
3.Loading Images from URLs
NSURL *url = [NSURL URLWithString:
@"http://image.weather.com/images/maps/current/curwx_600x405.jpg"];
UIImage *img = [UIImage imageWithData:
[NSData dataWithContentsOfURL:url]];
load itself asynchronously without blocking the main thread
// Create an asynchronous background queue
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:
^{
// Load the weather data
NSURL *weatherURL = [NSURL URLWithString:@"http://image.weather.com/images\
/maps/current/curwx_600x405.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:weatherURL];
// Update the image on the main thread using the main queue
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIImage *weatherImage = [UIImage imageWithData:imageData];
imageView.image = weatherImage;}];
No comments:
Post a Comment