你不知道的小技巧

一、iOS 加載網(wǎng)絡(luò)圖片不變形處理方法

此方法用在tableView里加載速度還是可以的,沒有卡頓現(xiàn)象

UIImageView *imgView =[[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 176, 136)];
[self.view addSubview:imgView];
NSString *imgUrlStr = @"http://photocdn.sohu.com/20090420/Img263501893.jpg";
[imageView sd_setImageWithURL:[NSURL URLWithString:imgUrlStr] placeholderImage:[UIImage imageNamed:@"deafult.png"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
   //圖片家完成,回調(diào)里裁剪圖片
   imageView.image = [self cutImage:image imgViewWidth:176 imgViewHeight:136];
}];

#pragma mark - 裁剪圖片
+ (UIImage *)cutImage:(UIImage*)image imgViewWidth:(CGFloat)width imgViewHeight:(CGFloat)height{
    CGSize newSize;
    CGImageRef imageRef = nil;
    
    if ((image.size.width / image.size.height) < (width / height)) {
        newSize.width = image.size.width;
        newSize.height = image.size.width * height /width;
        imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, fabs(image.size.height - newSize.height) / 2, newSize.width, newSize.height));
        
    } else {
        newSize.height = image.size.height;
        newSize.width = image.size.height * width / height;
        imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(fabs(image.size.width - newSize.width) / 2, 0, newSize.width, newSize.height));
    }
    return [UIImage imageWithCGImage:imageRef];
}


#pragma mark - 根據(jù)imgView的寬高獲得圖片的比例
+ (UIImage *)getImageFromUrl:(NSURL *)imgUrl imgViewWidth:(CGFloat)width imgViewHeight:(CGFloat)height{
    //根據(jù)網(wǎng)址將圖片轉(zhuǎn)化成image
    NSData *data = [NSData dataWithContentsOfURL:imgUrl];
    UIImage *tmpImg = [UIImage imageWithData:data];
    
    UIImage *newImage = [UIImage cutImage:tmpImg imgViewWidth:width imgViewHeight:height];
    return newImage;
}

二、配置工程名

你還能看到"Bundle Name" and "Bundle display name"都設(shè)置為動(dòng)態(tài)參數(shù)${PRODUCT_NAME}。
Bundle name - is folder name, where your app (including executable file and all resources) will be stored (Cool Program.app)。建議不要修改bundle name
Bundle display name - is what will be shown on iPhone screen,即當(dāng)你安裝該app到iPhone上顯示的name。
注意:Bundle Display name must correspond to Bundle name,即bundle display name和bundle name不能相差太遠(yuǎn)。例如bundle name設(shè)置為 TheApplication, 而 bundle display name設(shè)置為“金瓶梅”,則apple會(huì)拒絕你的app。
當(dāng)然,你也可以在info.plist file里修改這些屬性。

三、強(qiáng)制退出App

有這樣一個(gè)需求:不同意協(xié)議退出App
可以認(rèn)為制造Bug或采用:exit();、abort();、assert();、主動(dòng)制造一個(gè)崩潰

exit();

1.附加了關(guān)閉打開文件與返回狀態(tài)碼給執(zhí)行環(huán)境,并調(diào)用你用atexit注冊(cè)的返回函數(shù);
2.警告:不要使用exit函數(shù),調(diào)用exit會(huì)讓用戶感覺程序崩潰了,不會(huì)有按Home鍵返回時(shí)的平滑過渡和動(dòng)畫效果;
3.另外,使用exit可能會(huì)丟失數(shù)據(jù),因?yàn)檎{(diào)用exit并不會(huì)調(diào)用-applicationWillTerminate:方法和UIApplicationDelegate方法;
exit(1);//是異常退出;
exit(0);//是正常退出;

abort();

abort就像是點(diǎn)擊了home鍵有過渡動(dòng)畫(我沒發(fā)現(xiàn)效果)
1.這是默認(rèn)的程序結(jié)束函數(shù),這種方式可能會(huì)或可能不會(huì)以刷新與關(guān)閉打開的文件
或刪除臨時(shí)文件,這與你的設(shè)計(jì)有關(guān)。
2.abort就像是點(diǎn)擊了home鍵有過渡動(dòng)畫,使用的時(shí)建議選擇abort();

assert();

1.assert(1)為oc中的宏,只在debug模式下有用,當(dāng)條件成立時(shí),程序不會(huì)終止掉;當(dāng)條件不成立時(shí),程序終止。
2.oc程序中建議用assert(condition)函數(shù),推薦使用assert宏;
3.方法未實(shí)現(xiàn)完,放個(gè)ASSERT(0)調(diào)試運(yùn)行時(shí)執(zhí)行到此為報(bào)錯(cuò)中斷,好知道成員函數(shù)還沒寫完。
4.另一種情況是預(yù)防性的錯(cuò)誤檢查,在認(rèn)為不可能的執(zhí)行到的情況下加一句ASSERT(0),如果運(yùn)行到此,代碼邏輯或條件就可能有問題。
assert(0);
//作用是現(xiàn)計(jì)算表達(dá)式expression ,如果其值為假(即為0),那么它先向stderr打印一條出錯(cuò)信息,然后通過調(diào)用 abort 來終止程序運(yùn)行
assert(1);
//為oc中的宏,只在debug模式下有用,當(dāng)條件成立時(shí),程序不會(huì)終止掉;當(dāng)條件不成立時(shí),程序終止。

主動(dòng)制造一個(gè)崩潰

[[NSArray array] objectAtIndex:5];

這種方式自然是不推薦的啦!如果你有崩潰日志收集功能則會(huì)產(chǎn)生誤報(bào),有正規(guī)途徑還是走正規(guī)途徑吧~~~~

四、加載帶H5標(biāo)簽的富文本

1、用UILable承載顯示

    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[[aItem getContent] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    //改變行間距
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = 8;
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];

    _contentLab.attributedText = [[NSAttributedString alloc] initWithString:attributedString.string attributes:attributes];
    //獲取lable內(nèi)容高度:
   CGSize size = CGSizeMake(KWIDTH - 30, MAXFLOAT);//設(shè)置高度寬度的最大限度
   CGRect rect = [_contentLab.text boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]} context:nil];
    [_contentWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(rect.size.height);
    }];

2、用WKWebView承載顯示

   //設(shè)置代理 WKNavigationDelegate
   _contentWebView = [[WKWebView alloc] init];
    _contentWebView.navigationDelegate = self;
    [self.view addSubview:_contentWebView];
    [_contentWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
    }];
   
[_contentWebView loadHTMLString:[[SquareManager instance] getDetailContent] baseURL:nil];

通過代理方法更改字體大小、行間距等信息(其余代理方法用不到,沒有逐一列舉)

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    //修改字體大小 260%
    [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '260%'"completionHandler:nil];
}

五、iOS 相機(jī)相冊(cè)顯示英文的解決辦法

info.plist里面添加 Localized resources can be mixed 設(shè)置為 YES(表示是否允許應(yīng)用程序獲取框架庫內(nèi)語言)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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