一,1.使用文件創(chuàng)建(靜態(tài)方法)
UIImage *myImage = [UIImage imageNamed:@"bob"];
2.使用 URL 和原始數(shù)據(jù)(靜態(tài)方法)
NSData *imageData = [ NSData initWithBytes:imagePtr length:imageSize ]; 假設(shè) imagePtr 是一個(gè)指向原始數(shù)據(jù)的指針
UIImage* myImage = [ [ UIImage alloc ]initWithData:imageData ];
UIImage *myImage2 =[UIImage imageWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
3.使用Core Graphics (靜態(tài)方法)
UIImage* myImage3 = [UIImage imageWithCGImage:myCGImageRef];
4.使用文件(實(shí)例方法)
UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];
5.只要一個(gè)視圖對(duì)象的窗口的某些部分需要繪制,就可以調(diào)用它的drawRect方法。要在窗口內(nèi)部顯示一個(gè) UIImage 的內(nèi)容,可以調(diào)用該對(duì)象的 drawRect 方法:
- (void)drawRect:(CGRect)rect{
CGRect myRect;
myRect.origin.x = 0.0 ;
myRect.origin.y = 0.0;
myRect.size = myImage.size;
[myImage drawInRect:myRect];
}
二,設(shè)置圖像的方向,UIImage 有個(gè)只讀屬性imageOrientation 來(lái)標(biāo)識(shí)它的方向。
UIImageOrientation myOrientation = myImage.imageOrientation;
// typedef enum {
// UIImageOrientationUp, // default orientation 默認(rèn)方向
// UIImageOrientationDown, // 180 deg rotation 旋轉(zhuǎn)180度
// UIImageOrientationLeft, // 90 deg CCW 逆時(shí)針旋轉(zhuǎn)90度
// UIImageOrientationRight, // 90 deg CW 順時(shí)針旋轉(zhuǎn)90度
// UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 向上水平翻轉(zhuǎn)
// UIImageOrientationDownMirrored, // horizontal flip 向下水平翻轉(zhuǎn)
// UIImageOrientationLeftMirrored, // vertical flip 逆時(shí)針旋轉(zhuǎn)90度,垂直翻轉(zhuǎn)
// UIImageOrientationRightMirrored, // vertical flip 順時(shí)針旋轉(zhuǎn)90度,垂直翻轉(zhuǎn)
// } UIImageOrientation;
1. 根據(jù)給定得圖片,從其指定區(qū)域截取一張新得圖片
-(UIImage *)getImageFromImage{
//大圖bigImage
//定義myImageRect,截圖的區(qū)域
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
UIImage* bigImage= [UIImage imageNamed:@"圖片名字.jpg"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = 57.0;
size.height = 57.0;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
2. 等比率縮放
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
3.自定長(zhǎng)寬
- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
4.讀取網(wǎng)絡(luò)圖片
NSString *urlStr = [result valueForKey:@"profile_image_url"];
NSURL *url = [NSURLURLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imgData = [NSDatadataWithContentsOfURL:url];
UIImage *img = [UIImageimageWithData:imgData];
5.儲(chǔ)存圖片
儲(chǔ)存到app的文件里
UIImage *image;
NSString *path =[[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
6.讀取本地圖片
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"headimg.png"];
NSData *data = [NSDatadataWithContentsOfFile:path];
UIImage *img = [UIImageimageWithData:data];