繼基礎(chǔ)控件UITextView之后,期待的UIImageView,UIImage詳細(xì)介紹-->>保證你有意外收獲,如有問題歡迎指點(diǎn)。秩命。
1、UIImageView相關(guān)屬性
#######效果
2.gif
#######代碼
//初始化
UIImageView * imageView =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//設(shè)置圖片
/**
第一種:
[imageView setImage:[UIImage imageNamed:@"1.jpg"]];
//第二種:
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
UIImage *images=[UIImage imageWithContentsOfFile:filePath];
//[imageView setImage:images];
//第三種:
NSData *data=[NSData dataWithContentsOfFile:filePath];
UIImage *image2=[UIImage imageWithData:data];
[imageView setImage:image2];
*/
//imageView.image= [UIImage imageNamed:@"3"];
//設(shè)置圓角
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 10;
//設(shè)置邊框顏色和大小
imageView.layer.borderColor = [UIColor orangeColor].CGColor;
imageView.layer.borderWidth = 2;
//contentMode屬性:當(dāng)圖片小于imageView的大小處理圖片顯示
/**
這個(gè)屬性是用來設(shè)置圖片的顯示方式如居中、居右跑芳,是否縮放等突照,有以下幾個(gè)常量可供設(shè)定:
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill UIViewContentModeRedraw UIViewContentModeCenter UIViewContentModeTop UIViewContentModeBottom UIViewContentModeLeft UIViewContentModeRight UIViewContentModeTopLeft UIViewContentModeTopRight UIViewContentModeBottomLeft UIViewContentModeBottomRight
*/
imageView.contentMode = UIViewContentModeScaleAspectFill;
//為圖片添加單擊事件:一定要先將userInteractionEnabled置為YES帮非,這樣才能響應(yīng)單擊事件
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer:singleTap];
// 隱藏或者顯示圖片
imageView.hidden = NO;
// 設(shè)置透明度
imageView.alpha =0.9;
//播放一系列圖片
UIImage *image1 = [UIImage imageNamed:@"1.jpg"];
UIImage *image2 = [UIImage imageNamed:@"2"];
UIImage *image3 = [UIImage imageNamed:@"3"];
NSArray *imagesArray = @[image1,image2,image3];
imageView.animationImages = imagesArray;
// 設(shè)定所有的圖片在多少秒內(nèi)播放完畢
imageView.animationDuration = [imagesArray count];
// 不重復(fù)播放多少遍,0表示無數(shù)遍
imageView.animationRepeatCount = 0;
// 開始播放
[imageView startAnimating];
//[imageView sizeToFit]; // 將圖片尺寸調(diào)整為與內(nèi)容圖片相同
[self.view addSubview:imageView];
2讹蘑、UIImage相關(guān)屬介紹《接觸的不是很多 所有看了一些博客轉(zhuǎn)載》
文章出處-http://blog.csdn.net/iukey/article/details/7308433
UIKit中有一些類可以用來操縱單個(gè)圖像末盔,還有一個(gè)圖像類可以用來顯示圖像。Apple還提供了一種特殊的導(dǎo)航控制器座慰,用于從圖像庫中選擇圖像陨舱。UIImage類對圖像及其底層數(shù)據(jù)進(jìn)行封裝。它可以直接繪制在一個(gè)視圖內(nèi)版仔,或者作為一個(gè)圖像容器在另一個(gè)更大的圖像視圖容器中使用游盲。這個(gè)類類提供的方法可以用來從各種來源中載入圖像,在屏幕上設(shè)置圖片的方向蛮粮,以及提供有關(guān)圖像的信息益缎。對于簡單的圖形應(yīng)用,可以將UIImage對象用在視圖類的drawRect方法中蝉揍,用來繪制圖像和團(tuán)模板链峭。你可以用文件來初始化,也可以用url又沾、原始數(shù)據(jù)弊仪、或者一個(gè)Core Graphics圖像的內(nèi)容熙卡。靜態(tài)方法(類方法)和實(shí)例方法都有;這些方法可以引用并緩存已有的圖像內(nèi)容,也可以實(shí)例化新的圖像對象励饵,如何使用完全取決于應(yīng)用程序的需要驳癌。使用一個(gè)圖像的最簡單方法就是通過靜態(tài)方法。靜態(tài)方法不會去管理圖像的實(shí)例役听,與之相反颓鲜,他們提供了直接的接口,可以用來共享位于框架內(nèi)部的記憶體緩存對象典予。這有助于保持應(yīng)用程序的整潔甜滨,也會生去做清理工作的需要。靜態(tài)方法和實(shí)例方法都可以用來創(chuàng)建相同的對象瘤袖。
- 一衣摩、使用文件創(chuàng)建(靜態(tài)方法)
UIImage *myImage = [UIImage imageNamed:@"ppp"];
- 二、使用 URL 和原始數(shù)據(jù)(靜態(tài)方法)
NSData *imageData = [ NSData initWithBytes:image:imagePtr length:imageSize ];
// 假設(shè) imagePtr 是一個(gè)指向原始數(shù)據(jù)的指針
UIImage* myImage = [ [ UIImage alloc ]initWithData:imageData ];
UIImage *myImage2 =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
- 三捂敌、使用Core Graphics (靜態(tài)方法)
UIImage* myImage3 = [UIImage imageWithCGImage:myCGImageRef];
- 四艾扮、使用文件(實(shí)例方法)
UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];
- 五、使用 URL 和原始數(shù)據(jù)(實(shí)例方法)如果圖像存儲在內(nèi)存中占婉,你可以創(chuàng)建一個(gè)NSData 對象作為initWithData 方法的原始輸入泡嘴,來初始化一個(gè)UIImage對象。如果圖像是一張網(wǎng)絡(luò)圖片逆济,可以使用NSData來進(jìn)行預(yù)載酌予,然后用它來初始化UIImage對象:
UIImage *myImage5 =[ [ UIImage alloc]initWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]] ];
- 六、使用Core Graphics (實(shí)例方法)
UIImage* myImage6 = [[UIImage alloc]initWithCGImage:myCGImageRef];
- 七纹腌、顯示圖像當(dāng)視圖類的drawRect 方法被喚起時(shí)霎终,它們會調(diào)用內(nèi)部的回吐例程。與其他圖像類不同升薯,UIImage對象不能被當(dāng)成子 ,直接附著在其他視圖上莱褒,因?yàn)樗皇且粋€(gè)視圖類。反過來涎劈,一個(gè)UIView類則可以在視圖的drawRect例程中广凸,調(diào)用圖像的 drawRect 方法。這可以使得圖像顯在UIView類的顯示區(qū)域內(nèi)部蛛枚。只要一個(gè)視圖對象的窗口的某些部分需要繪制谅海,就可以調(diào)用它的drawRect方法。要在窗口內(nèi) 部顯示一個(gè) 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];}
注意不要在drawRect方法內(nèi)分配任何新對象扭吁,因?yàn)樗诿看未翱谥乩L時(shí)都被調(diào)用。 只有在視圖初次繪制時(shí),才會調(diào)用drawRect方法侥袜。要強(qiáng)制更新蝌诡,可以使用視圖類的 setNeedsDisplay 或者 setNeedsDisplayInRect 方法:
[myView setNeedsDisplay];
[myView setNeedsDisplayInRect:self.view];
- 八、繪制圖案如果圖像是一個(gè)圖案模板枫吧,你可以用UIImage類提供的另外一個(gè)方法 drawAsPatternInrect浦旱,在整個(gè)視圖區(qū)域重復(fù)繪制該圖像:
UIView* myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
[myImage drawInRect:myView.frame];
[self.view addSubview:myView];
- 九、方向一個(gè)圖像的方向九杂,決定了它在屏幕上如何被旋轉(zhuǎn)颁湖。因?yàn)閕Phone 能被以6種不同的方式握持,所以在方向改變時(shí)例隆,能夠?qū)D像做相應(yīng)的旋轉(zhuǎn)就十分必要了甥捺。UIImage 有個(gè)只讀屬性 imageOrientation 來標(biāo)識它的方向。
UIImageOrientation myOrientation = myImage.imageOrientation ;
可以設(shè)置以下方向:
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;
- 十、圖像尺寸你可以通過size屬性讀取一個(gè)圖像的尺寸鹿响,得到一個(gè)CGSize 結(jié)構(gòu),其中包含 wifth 和height 谷饿。
CGSize myImageSize = myImage.size;
3惶我、UIImage 轉(zhuǎn) NSdata
-(NSData *)getCoverImageDataWith:(NSString *)imgeUrl{
NSURL *aUrl = [[NSURL alloc]initWithString:imgeUrl];
NSData *aData = [[NSData alloc]initWithContentsOfURL:aUrl];
if (aData==nil) {
aData = UIImagePNGRepresentation([UIImage imageNamed:@"image_default"]) ;
}
return aData ;
}
未完待續(xù)。博投。绸贡。。