這段代碼動態(tài)的創(chuàng)建了一個UIButton,并且把相關(guān)常用的屬性都列舉了
一、使用文件創(chuàng)建(靜態(tài)方法)
UIImage *myImage = [UIImage imageNamed:@"imageName"];
二、使用 URL 和原始數(shù)據(jù)(靜態(tài)方法)
//NSData *imageData = [ NSData initWithBytes:imagePtr length:imageSize ]; //假設(shè) imagePtr 是一個指向原始數(shù)據(jù)的指針
//UIImage *myImage = [ [ UIImage alloc ]initWithData:imageData ];
//[NSURLURLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
三、使用Core Graphics (靜態(tài)方法)
UIImage* myImage3 = [UIImageimageWithCGImage:myCGImageRef];
四、使用文件(實(shí)例方法)
UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];
只要一個視圖對象的窗口的某些部分需要繪制,就可以調(diào)用它的drawRect方法。要在窗口內(nèi)部顯示一個 UIImage 的內(nèi)容,可以調(diào)用該對象的 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 有個只讀屬性imageOrientation 來標(biāo)識它的方向。
UIImageOrientation myOrientation = myImage.imageOrientation;
// typedef enum {
// UIImageOrientationUp, // default orientation 默認(rèn)方向
// UIImageOrientationDown, // 180 deg rotation 旋轉(zhuǎn)180度
// UIImageOrientationLeft, // 90 deg CCW 逆時針旋轉(zhuǎn)90度
// UIImageOrientationRight, // 90 deg CW 順時針旋轉(zhuǎn)90度
// UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 向上水平翻轉(zhuǎn)
// UIImageOrientationDownMirrored, // horizontal flip 向下水平翻轉(zhuǎn)
// UIImageOrientationLeftMirrored, // vertical flip 逆時針旋轉(zhuǎn)90度,垂直翻轉(zhuǎn)
// UIImageOrientationRightMirrored, // vertical flip 順時針旋轉(zhuǎn)90度,垂直翻轉(zhuǎn)
// } UIImageOrientation;
根據(jù)給定得圖片,從其指定區(qū)域截取一張新得圖片
-(UIImage )getImageFromImage{
//大圖bigImage
//定義myImageRect,截圖的區(qū)域
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
UIImage bigImage= [UIImage imageNamed:@"k00030.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;
}
等比率縮放
- (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;
}
自定長寬
- (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;
}
讀取網(wǎng)絡(luò)圖片
NSString *urlStr = [result valueForKey:@"profile_image_url"];
NSURL *url = [NSURLURLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imgData = [NSDatadataWithContentsOfURL:url];
UIImage *img = [UIImageimageWithData:imgData];
儲存圖片
//1) 儲存到app的文件里
UIImage *image;
NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
//這樣就把你要處理的圖片, 以image.png這個檔名存到app home底下的Documents目錄裡
//2)儲存到手機(jī)的圖片庫里
讀取本地圖片
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"headimg.png"];
NSData *data = [NSDatadataWithContentsOfFile:path];
UIImage *img = [UIImageimageWithData:data];