Iphone 支持以下圖像類型: PNG, JPG, THM, JPEG, TIF, TIFF, GIF, BMP, BMPF, ICO, CUR, XBM, PDF.
1.從應用程序包中加載圖像
myImage = [UIImage imageNamed:@"icon.png" ]; NSString *path = [[NSBundle mainBundle] pathForResource:@"icon" ofType"@"png" ]; myImage = [UIImage imageWithContentsOfFile:path]; |
2.從沙盒中加載圖像
查找Documents文件夾的標準方法:
NSArray *paths = [NSSearchPathForDirectoriesIndomains( NSDocumentDirectory, NSUserDomainMask, YES ) ]; return [paths lastObject]; NSString *documentsFolder() { return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents" ]; } |
如果要加載圖像,將圖像文件名添加刀返回的路徑中,並告訴UIImage使用這些內容創建一副新圖像.
下面代碼是從頂層文檔文件夾中加載一個名爲image.png 的文件,然後返回由該數據初始化的一個UIImage 實例.
1
2
|
path = [documentsFolder( ) stringByAppendingPathComponent:@"image.png"]; return [UIImage imageWithContentsOfFile:path]; |
3.從URL中加載圖像
UIImage類可以直接從NSData實例中加載圖像.但是他不能直接從URL字符串或者NSURL對象加載圖像.
所以,只能使用相關數據創建新圖像.
eg:
NSURL *url = [NSURL URLWithString:@"
http://www.baidu.com/image/image.png"]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; |
可以用一個簡單的方法來處理這個過程,只需提供一個可獲取UIImage的URL字符串即可.
下面的方法接受一個參數,即一個URL字符串
+ (UIImage *) imageFromURLString: (NSString *)urlstring { //This call is synchronous and blocking return [UIImage imageWithData:[NSData dataWithContentsOfURL:[ NSURL URLWithString:urlstring ]]]; } |
4.從相冊加載數據
UIImagePickerController 類幫主用戶從Iphone相冊中選擇圖像.
二.加載圖像文件
//-------------------
//ImageHelper-Files.h 文件
//--------------------
#import <UIKit/UIKit.h>
@interface ImageHelper : NSObject
@end
@interface ImageHelper (Files)
+ (UIImage *) imageNamed: (NSString *) aName;
+ (UIImage *) imageFromURLString: (NSString *) urlstring;
+ (NSArray *) DCIMImages;
+ (UIImage *) DCIMImageNamed: (NSString *) aName;
@end
//-------------------
//ImageHelper-Files.m 文件
//--------------------
#import "ImageHelper-Files.h"
@implementation ImageHelper
@end
NSString *documentsFolder()
{
return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}
NSString *bundleFolder()
{
return [[NSBundle mainBundle] bundlePath];
}
NSString *DCIMFolder()
{
return [NSHomeDirectory() stringByAppendingPathComponent:@"../../Media/DCIM"];
}
@implementation ImageHelper (Files)
+ (NSString *) pathForItemNamed: (NSString *) fname inFolder: (NSString *) path
{
NSString *file;
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
while (file = [dirEnum nextObject])
if ([[file lastPathComponent] isEqualToString:fname])
return [path stringByAppendingPathComponent:file];
return nil;
}
// Searches bundle first then documents folder
+ (UIImage *) imageNamed: (NSString *) aName
{
NSString *path = [ImageHelper pathForItemNamed:aName inFolder:bundleFolder()];
path = path ? path : [ImageHelper pathForItemNamed:aName inFolder:documentsFolder()];
if (!path) return nil;
return [UIImage imageWithContentsOfFile:path];
}
+ (UIImage *) imageFromURLString: (NSString *) urlstring
{
NSURL *url = [NSURL URLWithString:urlstring];
return [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
}
+ (NSArray *) DCIMImages
{
NSString *file;
NSMutableArray *results = [NSMutableArray array];
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:DCIMFolder()];
while (file = [dirEnum nextObject]) if ([file hasSuffix:@"JPG"]) [results addObject:file];
return results;
}
+ (UIImage *) DCIMImageNamed: (NSString *) aName
{
NSString *path = [ImageHelper pathForItemNamed:aName inFolder:DCIMFolder()];
return [UIImage imageWithContentsOfFile:path];
}
@end
//-------------------
TestBedViewController.h 文件
//--------------------
#import <UIKit/UIKit.h>
#import "ImageHelper-Files.h"
#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
#define SETIMAGE(X) [(UIImageView *)self.view setImage:X]
const int NUM_OPTIONS = 4;
int which = 0;
@interface TestBedViewController : UIViewController
@end
@implementation TestBedViewController
- (void) action: (id) sender
{
switch (which)
{
case 0:
// Load image from web
self.title = @"URL-based image";
SETIMAGE([ImageHelper imageFromURLString:@"http://image.weather.com/images/maps/current/curwx_600x405.jpg"]);
break;
case 1:
// use UIImage's imageNamed: with caching
self.title = @"imageNamed:";
SETIMAGE([UIImage imageNamed:@"BFlyCircle.png"]);
break;
case 2:
// Use the Image Helper version of imageNamed:
self.title = @"Image Helper";
SETIMAGE([ImageHelper imageNamed:@"icon.png"]);
break;
case 3:
// Load normal background image from bundle
self.title = @"Contents of file";
SETIMAGE([UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cover320x416" ofType:@"png"]]);
break;
default:
break;
}
NSString *next = [NSString stringWithFormat:@"Example %d", (which = (which + 1) % NUM_OPTIONS) + 1];
self.navigationItem.rightBarButtonItem = BARBUTTON(next, @selector(action:));
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Example 1", @selector(action:));
}
@end