【OC隨筆】寫給自己的一些小東西

前言:

在這里寫一些自己粗心容易忽略和忘記的東西,不定期更新……


素材網(wǎng):

聚合數(shù)據(jù)(各種免費API,要注冊實名認證):https://www.juhe.cn
atool(在線批量剪裁各種尺寸iOS、Android APP icon圖標):
http://www.atool.org/ios_logo.php
tool(在線代碼格式化(數(shù)據(jù)解析)):http://tool.oschina.net/codeformat/json
ydimage(在線批量剪裁各種尺寸iOS、Android APP icon圖標):http://ydimage.yidianhulian.com
阿里巴巴矢量圖(各種矢量圖標):http://www.iconfont.cn/plus/home/index
千庫網(wǎng)(各種免費下載的免修圖): http://588ku.com/print/0-0-pxnum-0-8-0-0-3/
千圖網(wǎng):http://www.58pic.com/app/
logoko (logo在線生成 純色):http://www.logoko.com.cn/index.php?
logaster (logo在線生成 彩色 推薦):https://www.logaster.cn/logo/
移動開發(fā)圖標規(guī)范:http://www.ui001.com/chicun/
oc轉(zhuǎn)swift: https://objectivec2swift.com/#/home/main
showapi(免費api,可以不實名認證):https://www.showapi.com/


生成高清二維碼:
/**
 * 根據(jù)CIImage生成指定大小的UIImage
 *
 * @param image CIImage
 * @param size 圖片寬度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.創(chuàng)建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到圖片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}


通過字符串的字數(shù)獲取應該要的size:
+(CGSize)textSize:(NSString*)str withWidth:(CGFloat)width withFont:(NSInteger)font{  
    CGSize size;  
    if (iosVersion>=7.0) {  
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:font]};  
        size = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;  
    }else{  
        size=[str sizeWithFont:[UIFont systemFontOfSize:font] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByCharWrapping|NSLineBreakByWordWrapping];  
    }  
    return size;  
}  

NSTextAlignmentCenter不生效:
cell = [[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

//改為

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“cell”];

tableview分區(qū)頭不懸停在頂部:
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, Height/1.6) style:UITableViewStylePlain];

//改為

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, Height/1.6) style:UITableViewStyleGrouped];

tableviewcell自定移動到最后一個
- (void)scrollTableToFoot:(BOOL)animated
{
    NSInteger s = [self.tableView numberOfSections];  //有多少組
    if (s<1) return;  //無數(shù)據(jù)時不執(zhí)行 要不會crash
    NSInteger r = [self.tableView numberOfRowsInSection:s-1]; //最后一組有多少行
    if (r<1) return;
    NSIndexPath *ip = [NSIndexPath indexPathForRow:r-1 inSection:s-1];  //取最后一行數(shù)據(jù)
    [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:animated]; //滾動到最后一行
}

去除tableview的灰線:
_tableView.separatorStyle = NO;
tabelview點擊后還原(不會一直顯示灰色):

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中寫

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

UIImage UIImageView 展示圖片 不變形 處理:

imageView 的contentMode 屬性 網(wǎng)上有個 很形象的例子

但是 盡量 其中 UIViewContentModeScaleAspectFill 會保證圖片比例不變,但是是填充整個ImageView的
但是 我使用的情況 比例嚴重不符合要求 所以 變形的非常明顯
這個時候 關鍵代碼就是
imageView.clipsToBounds = YES;
When YES, content and subviews are clipped to the bounds of the view. Default is NO.
這里的clip是修剪的意思,bounds是邊界的意思是,合起來就是:如果子視圖的范圍超出了父視圖的邊界,那么超出的部分就會被裁剪掉。 那么圖片 就會按比例顯示 盡管 圖片不會顯示全 這也是合理的吧哈哈


nib創(chuàng)建cell的兩種注冊方法:

血和淚的記憶

方法一:
//第一步:
[self.collectionView registerNib:[UINib nibWithNibName:@"QGLShareBtnCell" bundle:nil] forCellWithReuseIdentifier:@"QGLShareBtnCell”];
//第二步:
QGLShareBtnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QGLShareBtnCell" forIndexPath:indexPath];
方法二:
QGLIMGroupListCell *cell = (QGLIMGroupListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
if (cell == nil) 
{       
 cell= (QGLIMGroupListCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"QGLIMGroupListCell" owner:self options:nil]  lastObject];    
}

presentViewController彈出頁面反應遲鈍的問題

想要實現(xiàn)點擊tableview中的一個cell,彈出一個頁面,代碼如下:

HSLoginViewController *loginVC = [HSLoginViewController new];  
[self presentViewController:loginVC animated:YES completion:nil];       

結果頁面彈出速度非常慢,有時幾秒鐘才能彈出,又是根本不彈出,直到在頁面上隨意再次點擊一下才彈出。
將代碼做如下修改后,問題解決:

dispatch_async(dispatch_get_main_queue(), ^{  
            HSLoginViewController *loginVC = [HSLoginViewController new];  
            [self presentViewController:loginVC animated:YES completion:nil];  
        });  

由此推斷,presentViewController這個方法有可能不是在UI線程執(zhí)行的。


系統(tǒng)自帶分享功能

很多APP中都帶有社交分享功能,國內(nèi)較或的分享平臺有微信,IOS6后蘋果集成的新浪微博,還有IOS7后集成的騰訊微博。 在IOS中,實現(xiàn)社交分享可以自己編寫各個平臺的分享代碼,但代碼量較多,也可以利用iOS自帶的Social.framework,更可以利用第三方的分享框架,如友盟,ShareSDK等。

  1. 分享視圖控制器:SLComposeViewController
    SLComposeViewController的呈現(xiàn)在iPhone采用模態(tài)視圖,iPad則需要Popover視圖呈現(xiàn)。如果發(fā)送微博信息并不像彈出分享列表,再去選擇,而是在應用中直接進入寫評論的地方(iOS系統(tǒng)提供的UI畫面)。SLComposeViewController類可以幫助我們實現(xiàn)這個目的,SLComposeViewControlelr不僅可以撰寫weibo,還可以撰寫Tweet和Facebook。發(fā)送這樣的社交網(wǎng)絡信息一般會包含3中信息:初始文本、圖片和超鏈接,因此SLComposeViewController類設計3個相對方法:

setInitialText:(NSString *)text,設置初始文本內(nèi)容
addImage:(UIImage *)iamge,添加圖片
addURL:(NSURL *)url,添加超鏈接信息

使用SLComposeViewController來實現(xiàn)社交分享的具體步驟如下:

  1. 判斷設備是否可以向指定的分享平臺分享。
  2. 創(chuàng)建分享視圖控制器,指定分享平臺
  3. 設置分享內(nèi)容。
  4. 進入分享界面。5. 監(jiān)聽用戶操作。

本文主要介紹一下系統(tǒng)自帶的分享服務框架。 iOS系統(tǒng)為我們提供了兩個不同的類來實現(xiàn)分享服務。

需要用真機測試

項目中需要導入一個系統(tǒng)自帶頭文件 #import 我們在屏幕的點擊事件中來實現(xiàn)分享到微博
 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 首先判斷新浪分享是否可用
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        return;
    }
    // 創(chuàng)建控制器,并設置ServiceType
    SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    // 添加要分享的圖片
    [composeVC addImage:[UIImage imageNamed:@"Snip20150429_9"]];
    // 添加要分享的文字
    [composeVC setInitialText:@"share my CSDN Blog"];
    // 添加要分享的url
    [composeVC addURL:[NSURL URLWithString:@"http://blog.csdn.net/u011058732"]];
    // 彈出分享控制器
    [self presentViewController:composeVC animated:YES completion:nil];
    // 監(jiān)聽用戶點擊事件
    composeVC.completionHandler = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultDone) {
            NSLog(@"點擊了發(fā)送");
        }
        else if (result == SLComposeViewControllerResultCancelled)
        {
            NSLog(@"點擊了取消");
        }
    };
}

2 . 分享列表:UIActivityViewController
在iOS6之后系統(tǒng)為我們提供了一個分享列表視圖,它通過UIActivityViewController管理。蘋果設計它的主要目的是替換分享動作,分享動作選單是出于分享目的的動作選單。內(nèi)置活動列表項主要有一下幾個:

UIActivityTypePostToFacebook,Facebook活動列表項;
UIActivityTypePostToTwitter,Twitter活動列表項;
UIActivityTypePostToWeibo,新浪微博活動列表項;
UIActivityTypeMessage,iOS中的iMessage應用活動那個列表項;
UIActivityTypeMail,發(fā)送Mail活動列表項;
UIActivityTypePrint,共享打印活動列表項;
UIActivityTypeCopyToPasteboard,復制到剪切板活動列表項;
UIActivityTypeAssignToContact,訪問聯(lián)系人活動列表項;
UIActivityTypeSaveToCameraRoll,訪問設備上的相冊活動列表項;

使用UIActivityViewController我們可以選中需要分享的平臺,然后跳轉(zhuǎn)到分享內(nèi)容的邊界界面,具體的實現(xiàn)步驟如下:1. 設置分享內(nèi)容。2. 創(chuàng)建分享列表的控制器,并傳入分享內(nèi)容3. 推出分享視圖控制器。

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 設置分享內(nèi)容
    NSString *text = @"分享內(nèi)容";
    UIImage *image = [UIImage imageNamed:@"01"];    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    NSArray *activityItems = @[text, image, url];
    // 服務類型控制器
    UIActivityViewController *activityViewController =    [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityViewController animated:YES completion:nil];
    // 選中分享類型
    [activityViewController setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
        // 顯示選中的分享類型
        NSLog(@"act type %@",activityType);
        if (completed) {
            NSLog(@"ok");
        }else {
            NSLog(@"no ok");
        }
    }];
}

多線程各種問題

http://www.itdecent.cn/p/0b0d9b1f1f19


iOS 8中的Self Sizing Cells和Dynamic Type

http://www.cocoachina.com/ios/20140922/9717.html

在iOS 8中,蘋果引入了UITableView的一項新功能--Self Sizing Cells,對于不少開發(fā)者來說這是新SDK中一項非常有用的新功能。在iOS 8之前,如果想在表視圖中展示可變高度的動態(tài)內(nèi)容時,你需要手動計算行高,而Self Sizing Cells為展示動態(tài)內(nèi)容提供了一個解決方案。

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

優(yōu)化UITableViewCell高度計算的那些事

http://blog.sunnyxx.com/2015/05/17/cell-height-calculation/


iOS多線程

http://www.itdecent.cn/p/0b0d9b1f1f19

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,422評論 4 61
  • 生活很無趣,我也不知道我現(xiàn)在一個什么心態(tài)去面對生活,只想平淡的過,但是生活的壓力又使我不得不去,努力的去面對生活!...
    斉飛閱讀 665評論 0 1
  • 月半掩著,周圍散發(fā)著淡淡的微光。 每次都沿著同一條路散步,馬路兩邊停著不同顏色不同品牌的轎車,開著的超...
    H紅館閱讀 220評論 0 0
  • 如若此時我已故去 生命如雪花般消融 微笑也不會離開臉龐 只為你從原野上扯過的風 如若此時我已故去 生命如銀杏葉般飄...
    嵐皊閱讀 232評論 0 0
  • 當你認真談過一段感情,最后卻不得不分手了,后來你會很難再去喜歡別人,你不想花時間也不想去了解,就好比你寫一篇作文快...
    心受傷了閱讀 434評論 0 0

友情鏈接更多精彩內(nèi)容