關于讓圖檔在指定位置拉伸,有一個函數
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
(支援ios2--ios4)
這個函數是UIImage的一個執行個體函數,它的功能是建立一個内容可拉伸,而邊角不拉伸的圖檔,需要兩個參數,第一個是左邊不拉伸區域的寬度,第二個參數是上面不拉伸的高度。根據設定的寬度和高度,将接下來的一個像素進行左右擴充和上下拉伸。
注意:可拉伸的範圍都是距離leftCapWidth後的1豎排像素,和距離topCapHeight後的1橫排像素。
以上函數在apple官方文檔中定義為(Deprecated in iOS 5.0),雖經驗證在5.0.1上可以使用并不報錯,不過推薦在IOS5上使用以下函數:
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
(僅支援ios5)
相關代碼:
UIImage *bg = [[UIImage imageNamed:@"camera.png"]stretchableImageWithLeftCapWidth:30 topCapHeight:0];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(100, 200, 160, 60);
[self.view addSubview:bgView];
[bg release];
[bgView release];
UIImage *bg = [[UIImageimageNamed:@"camera.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(100, 200, 160, 60);
[self.view addSubview:bgView];
[bg release];
[bgView release];
參考網頁:https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIImage/stretchableImageWithLeftCapWidth:topCapHeight:
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/resizableImageWithCapInsets:
--yuzhang2
轉載于:https://www.cnblogs.com/ydhliphonedev/archive/2012/03/17/2403213.html