繼基礎控件UITextView之后,期待的UIImageView,UIImage詳細介紹-->>保證你有意外收獲,如有問題歡迎指點。。
1、UIImageView相關屬性
#######效果
2.gif
#######代碼
//初始化
UIImageView * imageView =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//設置圖片
/**
第一種:
[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"];
//設置圓角
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 10;
//設置邊框顏色和大小
imageView.layer.borderColor = [UIColor orangeColor].CGColor;
imageView.layer.borderWidth = 2;
//contentMode屬性:當圖片小于imageView的大小處理圖片顯示
/**
這個屬性是用來設置圖片的顯示方式如居中、居右,是否縮放等,有以下幾個常量可供設定:
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill UIViewContentModeRedraw UIViewContentModeCenter UIViewContentModeTop UIViewContentModeBottom UIViewContentModeLeft UIViewContentModeRight UIViewContentModeTopLeft UIViewContentModeTopRight UIViewContentModeBottomLeft UIViewContentModeBottomRight
*/
imageView.contentMode = UIViewContentModeScaleAspectFill;
//為圖片添加單擊事件:一定要先將userInteractionEnabled置為YES,這樣才能響應單擊事件
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer:singleTap];
// 隱藏或者顯示圖片
imageView.hidden = NO;
// 設置透明度
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;
// 設定所有的圖片在多少秒內播放完畢
imageView.animationDuration = [imagesArray count];
// 不重復播放多少遍,0表示無數遍
imageView.animationRepeatCount = 0;
// 開始播放
[imageView startAnimating];
//[imageView sizeToFit]; // 將圖片尺寸調整為與內容圖片相同
[self.view addSubview:imageView];
2、UIImage相關屬介紹《接觸的不是很多 所有看了一些博客轉載》
文章出處-http://blog.csdn.net/iukey/article/details/7308433
UIKit中有一些類可以用來操縱單個圖像,還有一個圖像類可以用來顯示圖像。Apple還提供了一種特殊的導航控制器,用于從圖像庫中選擇圖像。UIImage類對圖像及其底層數據進行封裝。它可以直接繪制在一個視圖內,或者作為一個圖像容器在另一個更大的圖像視圖容器中使用。這個類類提供的方法可以用來從各種來源中載入圖像,在屏幕上設置圖片的方向,以及提供有關圖像的信息。對于簡單的圖形應用,可以將UIImage對象用在視圖類的drawRect方法中,用來繪制圖像和團模板。你可以用文件來初始化,也可以用url、原始數據、或者一個Core Graphics圖像的內容。靜態方法(類方法)和實例方法都有;這些方法可以引用并緩存已有的圖像內容,也可以實例化新的圖像對象,如何使用完全取決于應用程序的需要。使用一個圖像的最簡單方法就是通過靜態方法。靜態方法不會去管理圖像的實例,與之相反,他們提供了直接的接口,可以用來共享位于框架內部的記憶體緩存對象。這有助于保持應用程序的整潔,也會生去做清理工作的需要。靜態方法和實例方法都可以用來創建相同的對象。
- 一、使用文件創建(靜態方法)
UIImage *myImage = [UIImage imageNamed:@"ppp"];
- 二、使用 URL 和原始數據(靜態方法)
NSData *imageData = [ NSData initWithBytes:image:imagePtr length:imageSize ];
// 假設 imagePtr 是一個指向原始數據的指針
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 (靜態方法)
UIImage* myImage3 = [UIImage imageWithCGImage:myCGImageRef];
- 四、使用文件(實例方法)
UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];
- 五、使用 URL 和原始數據(實例方法)如果圖像存儲在內存中,你可以創建一個NSData 對象作為initWithData 方法的原始輸入,來初始化一個UIImage對象。如果圖像是一張網絡圖片,可以使用NSData來進行預載,然后用它來初始化UIImage對象:
UIImage *myImage5 =[ [ UIImage alloc]initWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]] ];
- 六、使用Core Graphics (實例方法)
UIImage* myImage6 = [[UIImage alloc]initWithCGImage:myCGImageRef];
- 七、顯示圖像當視圖類的drawRect 方法被喚起時,它們會調用內部的回吐例程。與其他圖像類不同,UIImage對象不能被當成子 ,直接附著在其他視圖上,因為他不是一個視圖類。反過來,一個UIView類則可以在視圖的drawRect例程中,調用圖像的 drawRect 方法。這可以使得圖像顯在UIView類的顯示區域內部。只要一個視圖對象的窗口的某些部分需要繪制,就可以調用它的drawRect方法。要在窗口內 部顯示一個 UIImage 的內容,可以調用該對象的 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方法內分配任何新對象,因為他在每次窗口重繪時都被調用。 只有在視圖初次繪制時,才會調用drawRect方法。要強制更新,可以使用視圖類的 setNeedsDisplay 或者 setNeedsDisplayInRect 方法:
[myView setNeedsDisplay];
[myView setNeedsDisplayInRect:self.view];
- 八、繪制圖案如果圖像是一個圖案模板,你可以用UIImage類提供的另外一個方法 drawAsPatternInrect,在整個視圖區域重復繪制該圖像:
UIView* myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
[myImage drawInRect:myView.frame];
[self.view addSubview:myView];
- 九、方向一個圖像的方向,決定了它在屏幕上如何被旋轉。因為iPhone 能被以6種不同的方式握持,所以在方向改變時,能夠將圖像做相應的旋轉就十分必要了。UIImage 有個只讀屬性 imageOrientation 來標識它的方向。
UIImageOrientation myOrientation = myImage.imageOrientation ;
可以設置以下方向:
typedef enum {
UIImageOrientationUp, // default orientation 默認方向
UIImageOrientationDown, // 180 deg rotation 旋轉180度
UIImageOrientationLeft, // 90 deg CCW 逆時針旋轉90度
UIImageOrientationRight, // 90 deg CW 順時針旋轉90度 UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 向上水平翻轉
UIImageOrientationDownMirrored, // horizontal flip 向下水平翻轉
UIImageOrientationLeftMirrored, // vertical flip 逆時針旋轉90度,垂直翻轉
UIImageOrientationRightMirrored, // vertical flip 順時針旋轉90度,垂直翻轉
} UIImageOrientation;
- 十、圖像尺寸你可以通過size屬性讀取一個圖像的尺寸,得到一個CGSize 結構,其中包含 wifth 和height 。
CGSize myImageSize = myImage.size;
3、UIImage 轉 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 ;
}
未完待續。。。。