亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS開發(fā)image背景圖片拉伸問題解決分析

 更新時間:2023年07月27日 11:31:31   作者:山水域  
這篇文章主要為大家介紹了iOS開發(fā)image背景圖片拉伸問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

(如果是imageView的圖片拉伸問題,可直接看文章結尾,OC和Swift)

在開發(fā)中聊天、按鈕等背景圖片,UI設計師可以僅設計其邊框樣式,然后通過代碼就行處理,以適應聊天文字的大小或按鈕的大小。

這樣不僅可以使安裝包更輕巧而且可以更靈活的使用圖片;

方法一:

即將棄用方法這個函數(shù)是UIImage的一個實例函數(shù),它的功能是創(chuàng)建一個內容可拉伸,而邊角不拉伸的圖片,需要兩個參數(shù),第一個是左邊不拉伸區(qū)域的寬度,第二個參數(shù)是上面不拉伸的高度。

根據(jù)設置的寬度和高度,將接下來的一個像素進行左右擴展和上下拉伸。

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;

注意:可拉伸的范圍都是距離leftCapWidth后的1豎排像素,和距離topCapHeight后的1橫排像素。

   UIImage *image = [UIImage imageNamed:@"圓角矩形-7-拷貝"];
    //原始圖片樣式添加
    self.originalImageView.image = image;
    //沒處理好的圖片
    self.badImageView.image = image;
//圖片處理后的 將被棄用 方法一:
//這個函數(shù)是UIImage的一個實例函數(shù),它的功能是創(chuàng)建一個內容可拉伸,而邊角不拉伸的圖片,需要兩個參數(shù),第一個是左邊不拉伸區(qū)域的寬度,第二個參數(shù)是上面不拉伸的高度。
//根據(jù)設置的寬度和高度,將接下來的一個像素進行左右擴展和上下拉伸。
    [self.textImageView setImage:[image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5]];
//注意:可拉伸的范圍都是距離leftCapWidth后的1豎排像素,和距離topCapHeight后的1橫排像素。

圖片講解

可拉伸的范圍都是距離leftCapWidth后的1豎排像素,和距離topCapHeight后的1橫排像素。

方法二:

iOS 5 推出

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets; 
// create a resizable version of this image. the interior is tiled when drawn.
   UIImage *image = [UIImage imageNamed:@"圓角矩形-7-拷貝"];
    //原始圖片樣式添加
    self.originalImageView.image = image;
    //沒處理好的圖片
    self.badImageView.image = image;
//處理圖片 iOS 5 方法三:
            CGFloat width = image.size.width;
            CGFloat height = image.size.height;
            CGFloat top = height/2.0f - 0.5f; // 頂端蓋高度
            CGFloat bottom = height/2.0f - 0.5f ; // 底端蓋高度
            CGFloat left = width/2.0f - 0.5f; // 左端蓋寬度
            CGFloat right = width/2.0f - 0.5f; // 右端蓋寬度
            UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
            //創(chuàng)建 一個可變的image版本,內部平鋪,類:UIImageResizingModeTile模式;
            self.textImageView.image = [image resizableImageWithCapInsets:insets];

圖片講解

中間的框框是復制平鋪區(qū)域,在本工程中為2px大小,Cap部分(即線的區(qū)域)為保留樣式

方法三:

iOS 6 方法

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
 // the interior is resized according to the resizingMode
   UIImage *image = [UIImage imageNamed:@"圓角矩形-7-拷貝"];
    //原始圖片樣式添加
    self.originalImageView.image = image;
    //沒處理好的圖片
    self.badImageView.image = image;
            //處理圖片 iOS 6 方法二:
            CGFloat width = image.size.width;
            CGFloat height = image.size.height;
            CGFloat top = height/2.0f - 0.5f; // 頂端蓋高度
            CGFloat bottom = height/2.0f - 0.5f ; // 底端蓋高度
            CGFloat left = width/2.0f - 0.5f; // 左端蓋寬度
            CGFloat right = width/2.0f - 0.5f; // 右端蓋寬度
            UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
            // 指定為拉伸模式,伸縮后重新賦值
            //UIImageResizingModeStretch:拉伸模式,通過拉伸UIEdgeInsets指定的矩形區(qū)域來填充圖片
            //UIImageResizingModeTile:平鋪模式,通過重復顯示UIEdgeInsets指定的矩形區(qū)域來填充圖片
            _textImageView.image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeTile];
// Swift
let image = UIImage(named: "rounded_rectangle_7_copy")
if image != nil {  
    self.originalImageView.image = image  
    self.badImageView.image = image  
} else {  
    self.originalImageView.image = image  
    self.badImageView.image = image  
}
let width = image?.size.width ?? 0  
let height = image?.size.height ?? 0
let top = height / 2 - 0.5  
let bottom = height / 2 - 0.5  
let left = width / 2 - 0.5  
let right = width / 2 - 0.5
let insets = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
if image != nil {  
    let resizingMode = UIImage.resizableImageResizingMode.tile  
    self.textImageView.image = UIImage(resizableImageWithCapInsets: insets, resizingMode: resizingMode)  
} else {  
    self.textImageView.image = UIImage(resizableImageWithCapInsets: insets, resizingMode: resizingMode)  
}

補充知識

關于imageView的圖片拉伸問題,在這里稍作總結,希望可以給你提供幫助。

typedef NS_ENUM(NSInteger, UIViewContentMode) {
    UIViewContentModeScaleToFill,
    UIViewContentModeScaleAspectFit,      
// contents scaled to fit with fixed aspect. remainder is transparent
    UIViewContentModeScaleAspectFill,    
 // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    UIViewContentModeRedraw,             
 // redraw on bounds change (calls -setNeedsDisplay)
    UIViewContentModeCenter,             
 // contents remain same size. positioned adjusted.
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
};
//使用方法
[ImageView setContentMode:UIViewContentModeScaleAspectFit];
//OR
ImageView.contentMode = UIViewContentModeScaleAspectFit;
//以下方法,圖片保持原有大小比例的情況下,展示在ImageView的上下左右等位置;如果視圖大小小于圖片的尺寸,則圖片會超出視圖邊界;
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
    UIViewContentModeCenter,  
    UIViewContentModeScaleToFill,  //根據(jù)視圖的比例去拉伸圖片內容。圖片可能變形;
    UIViewContentModeScaleAspectFit,  //保持圖片內容的縱橫比例,來適應視圖的大小。
    UIViewContentModeScaleAspectFill,   //用圖片內容來填充視圖的大小,多余得部分可以被修剪掉來填充整個視圖邊界。 
    UIViewContentModeRedraw,     //這個選項是單視圖的尺寸位置發(fā)生變化的時候通過調用setNeedsDisplay方法來重新顯示。       

以上就是iOS開發(fā)image背景圖片拉伸問題解決分析的詳細內容,更多關于iOS image背景圖片拉伸的資料請關注腳本之家其它相關文章!

相關文章

  • iOS點擊文字按鈕變轉圈加載效果

    iOS點擊文字按鈕變轉圈加載效果

    這篇文章主要為大家詳細介紹了iOS點擊文字按鈕變轉圈加載效果的相關資料,感興趣的小伙伴們可以參考一下
    2016-05-05
  • iOS實現(xiàn)轉盤效果

    iOS實現(xiàn)轉盤效果

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)轉盤效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • IOS中UIWebView、WKWebView之JS交互

    IOS中UIWebView、WKWebView之JS交互

    本篇文章主要介紹了IOS中UIWebView、WKWebView之JS交互,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • iOS10 推送完整剖析和注意事項

    iOS10 推送完整剖析和注意事項

    這篇文章主要為大家詳細介紹了iOS10 推送完整剖析和注意事項,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • iOS開發(fā)存儲應用程序Info.plist知識全面詳解

    iOS開發(fā)存儲應用程序Info.plist知識全面詳解

    這篇文章主要為大家介紹了iOS開發(fā)存儲應用程序Info.plist知識全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • IOS HTTP請求的常見狀態(tài)碼總結

    IOS HTTP請求的常見狀態(tài)碼總結

    這篇文章主要介紹了IOS HTTP請求的常見狀態(tài)碼總結的相關資料,需要的朋友可以參考下
    2017-02-02
  • iOS模糊效果的實現(xiàn)方法

    iOS模糊效果的實現(xiàn)方法

    這篇文章主要為大家詳細介紹了iOS模糊效果的實現(xiàn)方法,利用系統(tǒng)的CoreImage濾鏡、UIImage ImageEffects分類和UIVisualEffectView實現(xiàn)模糊效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS微信分享后關閉發(fā)送成功提示并返回應用

    iOS微信分享后關閉發(fā)送成功提示并返回應用

    這篇文章主要為大家詳細介紹了iOS微信分享后關閉發(fā)送成功提示并返回應用的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS10 適配-Xcode8問題總結及解決方案

    iOS10 適配-Xcode8問題總結及解決方案

    這篇文章主要介紹了iOS10 適配-Xcode8問題總結的相關資料,這里整理了遇到的幾種問題,并給出解決方案,需要的朋友可以參考下
    2016-11-11
  • iOS如何保持程序在后臺長時間運行

    iOS如何保持程序在后臺長時間運行

    這篇文章主要為大家詳細介紹了iOS如何保持程序在后臺長時間運行,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論