重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
首先要在項目目錄下導入一張圖片,如 testImage@2x.png
創新互聯是一家專業從事網站建設、網絡營銷、微信小程序、網站運營為一體的建站企業;在網站建設告別千篇一律,告別似曾相識,這一次我們重新定義網站建設,讓您的網站別具一格。成都響應式網站建設公司,實現全網營銷!一站適應多終端,一樣的建站,不一樣的體驗!
然后,在viewController.view上,添加一個子視圖UIImageView imageVeiw.
[self.view addSubview:imageVeiw];
然后使用圖片文件生成圖片對象image
UIImage *image = [UIImage imageNamed:@"testImage"];//不需要包含后綴'@2x.png'
然后,將圖片加載到imageView中:
[imageVeiw setImage:image];
至此,就能在屏幕上顯示一張圖片了。
一個圖片的地址放在瀏覽中能正常加載出來,但使用SDWebImage加載該圖片地址的時候卻加載不出來。
之前遇到這個問題有兩種情況
瀏覽器在加載兩類圖片的時候能夠對圖片的地址進行處理,然后正常加載出來。
然而SDWebImage卻沒有對圖片地址進行像瀏覽器那樣的處理,這時需要我們自己對圖片地址進行處理。
對于第一種圖片地址我們需要對漢字進行轉碼,方法:
對于第二種圖片地址我們需要將反斜杠轉成斜杠,方法:
第二種處理方法是用"/"替換地址中的"\",然而字符串"\"不能直接使用,需要使用\\,因為\后帶一些字符是轉義字符。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgImage"]];?
創建并設置默認圖, 也可以
UIImageView*imageView = [[UIImageView alloc] init];
imageView.image= [UIImageimageNamed:@"bgImage"];
還可以這樣先設置imageview的大, 在設置圖片
UIImageView*imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(0,144,SCREEN_Width,50))];
imageView.image= [UIImageimageNamed:@"bgImage"];
由此可看imageview的frame可以這樣設置
imageView.frame=CGRectMake(0,144,SCREEN_Width,50);
通常我們使用的的imageview都會添加圓角邊框
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius=25;
imageView.layer.borderColor = [UIColor blueColor].CGColor;
imageView.layer.borderWidth=1;
這個圓角和邊框像view和label以及button的設置方式都是一樣的 當然imageview也一樣
imageView.backgroundColor= [UIColorclearColor]; 圖片設置背景顏色, 我通常使用clearColor ?透明
?imageView.userInteractionEnabled = YES; 圖片設置成可交互, 設置為NO則不能交互
[self.viewaddSubview: imageView]; 添加視圖也可叫做顯示視圖
設置圖片內容的布局方式 imageView.contentMode
這個屬性是用來設置圖片的顯示方式,如居中、居右,是否縮放等
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIViewContentMode contentMode枚舉類型
? ? (1)? UIViewContentModeScaleToFill; ? ??默認,對圖片進行拉伸處理(不是按比例),是充滿bouns
? (2)? UIViewContentModeScaleAspectFit; ? ??按原圖比例進行拉伸,是圖片完全展示在bouns中
? ? (3)? UIViewContentModeScaleAspectFill; ? ??按原圖比例填充,使圖片展示在bouns中,可能只顯示部分
? ? (4)? UIViewContentModeRedraw; ? ??重劃邊界變化(重設 - setNeedsDisplay)
? ? (5)? UIViewContentModeCenter; ? ??圖片顯示在imageview的正中間,原圖大小
? ? (6)? UIViewContentModeTop; ? ??圖片顯示在imageview的上部,原圖大小
? ? (7)? UIViewContentModeBottom; ? ??圖片顯示在imageview的下部,原圖大小
? ? (8)? UIViewContentModeLeft; ? ??圖片顯示在imageview的左部,原圖大小
? ? (9)? UIViewContentModeRight; ? ??圖片顯示在imageview的右部,原圖大小
? ? (10)? UIViewContentModeTopLeft; ? ??圖片顯示在imageview的左上部,原圖大小
? ? (11)? UIViewContentModeTopRight; ? ??圖片顯示在imageview的右上部,原圖大小
? ? (12)? UIViewContentModeBottomLeft; ? ??圖片顯示在imageview的左下部,原圖大小
? ? (13)? UIViewContentModeBottomRight; ? ??圖片顯示在imageview的右下部,原圖大小
imageView.alpha = 1.0; ? ?設置圖片透明度
???NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
???NSString *path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];
???NSString *path3 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"jpg"];
???imageView.animationImages = @[[UIImage imageWithContentsOfFile:path1],[UIImage imageWithContentsOfFile:path2],[UIImage imageWithContentsOfFile:path3]];
???imageView.animationDuration = 5.0f; ? ?設置循環一次的時間
???imageView.animationRepeatCount = 0;? ? // 設置循環次數(0為無線循環)
???[imageView startAnimating];? ? ? ? ? ? // 開始動畫
???[imageView stopAnimating];? ? ? ? ? ? ? // 停止動畫
NSData *imageData = [NSData dataWithContentsOfFile:path];
UIImage *image4 = [UIImage imageWithData:imageData];
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
UIImage *image2 = [UIImage imageWithContentsOfFile:path];
ImageView.hidden?=?NO;????隱藏或者顯示圖片?YES為隱藏
[ImageView?sizeToFit];????將圖片尺寸調整為與內容圖片相同
UITapGestureRecognizer?*singleTap = [[UITapGestureRecognizer?alloc]?initWithTarget:self?action:@selector(tapImageView:)];?//?設置手勢
[ImageView?addGestureRecognizer:singleTap];?//?給圖片添加手勢
以上都是使用 Image 加載本地圖片資源,但是 SwiftUI 中的 Image 沒有提供直接加載 URL 方式的圖片顯示,那么如何在 SwiftUI 中讓 Image 加載網絡圖片呢,可以采用異步加載網絡圖片數據,由 data 轉換成 UIimage ,再給 Image 展示
但是這種異步加載圖片的方式在 Widget 中卻失效了, Image 顯示不了圖片。
在 TimelineProvider 的 getTimeline 中 completion(timeline) 執行完之后,不再支持圖片的異步回調了,所以必須在數據請求回來的處理中采用同步方式,將圖片的 data 獲取,轉換成 UIimage ,在賦值給 Image 展示。
接下里給 iOS14 Widget小組件開發實踐2——自定義Widget 里搭建的古詩視圖增加一個網絡封面圖片顯示,效果如下:
因為這個免費的 API 接口沒有返回圖片封面數據,所以就自己網上找個圖片用來測試。關于圖片請求的時機,這里我是將它放在了 API 接口回調后處理 json 轉 model 的這一步:
最后在給 PoetryWidgetView 布局界面:
SwitUI-實現URL圖片顯示