屏幕適配和點九圖:
一、點九圖:
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 150,50)];
UIImage * image = [UIImage imageNamed:@"abc.png"];
方法一:適用於任何圖片:
//給圖像設置放大邊界 距左右11像素 距上下14像素
UIEdgeInsets insets = UIEdgeInsetsMake(14, 5, 14, 5);
// 指定圖片放大的模式以及邊界
image = [image resizableImageWithCapInsets:insetsresizingMode:UIImageResizingModeStretch];
//方法二、適用於規則的圖形(各個邊角相同)
/image = [image stretchableImageWithLeftCapWidth:image.size.width*.5topCapHeight:image.size.height*.5]; /
[btn setBackgroundImage:image forState:UIControlStateNormal];
[self.view addSubview:btn];
二、屏幕適配:
1、在appdelegate中創建此方法,在啓動時調用此方法,獲取://獲取當前屏幕與基準屏幕的寬之間的比值以及高之間的比值
-(void)abc{
if ( [UIScreen mainScreen].bounds.size.height > 480) {
self.scaleX = [UIScreen mainScreen].bounds.size.width/320;
self.scaleY = [UIScreen mainScreen].bounds.size.height/568;
}else{
self.scaleX = 1;
self.scaleY = 1;
}
}
2、在ViewController中
//重寫cgrectmake方法
CG_INLINE CGRect
CGRectAuToMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
//根據寬高比將現有的位置信息放大
AppDelegate * app = [UIApplication sharedApplication].delegate;
CGRect rect;
rect.origin.x = x*app.scaleX; rect.origin.y = y*app.scaleY;
rect.size.width = width*app.scaleX; rect.size.height = height*app.scaleY;
return rect;
}
3、在創建空間時用重寫的cgrectmake方法創建控件;如果有xib拖拽的空間需要遍歷子控件;改變子控件frame,方法如下:
-(void)layoutView:(UIView *)view{
for (UIView * v in view.subviews) {
[v setFrame:CGRectAuToMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width, v.frame.size.height)];
}
}
加載GIF圖片有兩種方法:
一、在webview上加載文件
//加載到 內存中
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201211/03/20121103120709_EBnZ3.thumb.600_0.gif
"]];
// 加載到 myWebView
[self.myWebView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
二、用imageview加載:配置工具包
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *myWebView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageview_gif = [[UIImageViewalloc]initWithFrame:CGRectMake(0, 300, 320, 300)];
[self.view addSubview:self.imageview_gif];
[self create];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)create
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"可愛" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSDictionary *gifLoopCount = [NSDictionarydictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0] , (NSString *)kCGImagePropertyGIFLoopCount,nil
];
NSDictionary * gifProperties = [NSDictionarydictionaryWithObject:gifLoopCount forKey:(NSString*)kCGImagePropertyGIFDictionary] ;
//__bridge爲橋接OC語言,使用了就不需要釋放,用的c語言;
CGImageSourceRef gif = CGImageSourceCreateWithData((__bridge CFDataRef)(data), (__bridge CFDictionaryRef)gifProperties);
CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));
NSLog(@"%@",gifprops);
NSInteger count =CGImageSourceGetCount(gif);
CFDictionaryRef gifDic = CFDictionaryGetValue(gifprops,kCGImagePropertyGIFDictionary);
NSNumber * delay = CFDictionaryGetValue(gifDic,kCGImagePropertyGIFDelayTime);
NSNumber * w = CFDictionaryGetValue(gifprops,@"PixelWidth");
NSNumber * h =CFDictionaryGetValue(gifprops,@"PixelHeight");
NSTimeInterval totalDuration = delay.doubleValue * count;
CGFloat pixelWidth = w.intValue;
CGFloat pixelHeight = h.intValue;
NSMutableArray *images = [[NSMutableArray alloc] init];
for(int index=0;index
{
CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, nil);
UIImage *img = [UIImage imageWithCGImage:ref];
[images addObject:img];
CFRelease(ref);
}
CFRelease(gifprops);
CFRelease(gif);
[_imageview_gif setBounds:CGRectMake(0, 0, pixelWidth, pixelHeight)];
[_imageview_gif setAnimationImages:images];
[_imageview_gif setAnimationDuration:totalDuration];
[_imageview_gif startAnimating];
}
學習ios 重要還是要理清楚思路 在做或者看老師代碼的時候 自己多想想爲什麼 不要自己看着就抄 另外還是要推薦一下 藍懿IOS這個培訓機構 和劉國斌老師劉國斌老師還是很有名氣的,聽朋友說劉老師成立了藍懿iOS,,老師講課方式很獨特,能夠儘量讓每個人都能弄明白,有的比較難懂的地方,如果有的地方還是不懂得話,老師會換個其它方法再講解,這對於我們這些學習iOS的同學是非常好的,多種方式的講解會理解得更全面,這個必須得給個贊,嘻嘻,還有就是這裏的學習環境很好,很安靜,可以很安心的學習,安靜的環境是學習的基礎,小班講課,每個班20幾個學生,學習氛圍非常好,每天都學到9點多才離開教室,練習的時間很充裕,而且如果在練習的過程中有什麼困難,隨時可以向老師求助,不像其它機構,通過視頻教學,有的甚至學完之後都看不到講師本人,問點問題都不方便,這就是藍懿與其它機構的區別,相信在劉國斌老師的細心指導下,每個藍懿學員都能找到滿意的工作,加油!
寫博客第八十天;
QQ:565803433