IOS開發(fā)隨筆

1,后臺返回圖片太大了.

用collectionView, 后臺返回圖片太大, 滑動就會有點卡 內(nèi)存還會有點大
以下是我做的處理 -- 異步裁剪圖片

-(void)setModel:(GoodsDetailModel *)model{
    _model = model;

//  [_wineImagev sd_setImageWithURL:BeURL(GetFullUrlStr(model.imgPath)) placeholderImage:[UIImage imageNamed:@"icon_loading_wine.png"]];
    
     SDWebImageManager* webManger = [SDWebImageManager sharedManager];
  
    [webManger downloadImageWithURL:BeURL(GetFullUrlStr(model.imgPath)) options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            if (image.size.width > 300) {
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    CGFloat scale = image.size.width / 300;
                    UIImage*img = UIImageScaleToSize(image,CGSizeMake(300, image.size.height / scale));
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.wineImagev.image = img;
                    });
                });
            }else{
                self.wineImagev.image = image;
            }
        }];

//修改圖片尺寸
UIImage * UIImageScaleToSize(UIImage *img, CGSize size){

    UIGraphicsBeginImageContext(size);
   
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
 
    return scaledImage;
}

如果內(nèi)存還是太大的話 可以這樣設置SD

  [[SDImageCache sharedImageCache] setShouldCacheImagesInMemory:NO];
  [[SDImageCache sharedImageCache] setShouldDecompressImages:NO];
  [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];

2, 用TTTAttributedLabel 給label中關(guān)鍵詞加點擊事件

屏幕快照 2016-09-08 17.49.57.png
 _attributeLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(10, 50, SCREEN_SIZE.width - 20, 200)];
    _attributeLabel.numberOfLines = 0;
    _attributeLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _attributeLabel.delegate = self;
    
    NSMutableAttributedString* attStr = [[NSMutableAttributedString alloc]initWithString:@"多少年都未曾忘記2005年清晨哪位和我視頻的女網(wǎng)友:夜未央袱结。那海藻般的頭發(fā)鲫凶,白色的棉布內(nèi)衣蜕琴,如同安妮寶貝筆下那個蝶念,慵懶靖榕,干凈语御,純凈的女子一般梯醒。當她對我微微一笑定页,我即刻頭暈目眩踢械,感覺生命仿佛被一道白光刺穿酗电,涉世未深的我第一次感覺到了成熟女人的魅力,依依不舍的關(guān)掉視頻后内列,我昏睡了整整10天 -- 殺馬特強子"];
    
    NSString* keyStr1 = @"夜未央", * keyStr2 = @"安妮寶貝";
    
    NSRange range1 = [attStr.string rangeOfString:keyStr1];
    NSRange range2 = [attStr.string rangeOfString:keyStr2];
    
    //設置整體屬性字符串
    [attStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:RangeAll(attStr)];
    
    NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc]init];
    style.lineSpacing = 5;
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:RangeAll(attStr)];
    
    [_attributeLabel setText:attStr];
    
    //正常狀態(tài)下的屬性
    _attributeLabel.linkAttributes = @{
                                       (NSString *)kCTForegroundColorAttributeName: [UIColor orangeColor],
                                       (NSString *)kCTUnderlineStyleAttributeName: @(YES)
                                       };
    //點下去時的屬性
    _attributeLabel.activeLinkAttributes = @{
                                             (NSString *)kCTForegroundColorAttributeName: [UIColor cyanColor]
                                             };
    //不活躍時的屬性 比如彈出alert時
    _attributeLabel.inactiveLinkAttributes = _attributeLabel.linkAttributes;
    
    //添加點擊事件 傳入字典 在代理或block拿到
    TTTAttributedLabelLink* link1 = [_attributeLabel addLinkToTransitInformation:@{@"001":keyStr1} withRange:range1];
    
    [_attributeLabel addLinkToTransitInformation:@{@"002":keyStr2} withRange:range2];
    
    
    //可以使用點擊事件block 或者 使用代理
    //    link1.linkTapBlock =  ^(TTTAttributedLabel *attributeLabel, TTTAttributedLabelLink *labeLink){
    //
    //        NSDictionary* dic = labeLink.result.components;
    //
    //        if (dic) {
    //
    //        }
    //    };
    
    [self.view addSubview:_attributeLabel];

#pragma mark - 點擊事件代理
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithTransitInformation:(NSDictionary *)components{

    if (components) {
        
        NSString* msg = [NSString stringWithFormat:@"點擊了%@",[[components allValues] lastObject]];
        
        [self alertMessage:msg];
    }
}

注意:
cell 在重用的時候撵术,之前設的 link 都沒有清除

必須使用: self.attributedLabel.text = attributedString;

不要用: self.attributedLabel.attributedText = attributedString;

TTTAttributedLabel下載地址
https://github.com/TTTAttributedLabel/TTTAttributedLabel

3.用LDRefresh 實現(xiàn)防淘寶商品詳情中查看寶貝圖文詳情方式tableview 與 webview 的切換 https://github.com/SNTD/LDRefresh

LDRefresh.gif

4,用UISegmentedControl 通過addChildViewController 控制兩個控制器的切換

//首先用一個主控制器 管理兩個子控制器

   self.fristVC = [[HomeMutableSaleVC alloc] init];
   self.secondVC = [[HomeMapController alloc] init];
    
    self.fristVC.view.frame = CGRectMake(0, CGRectGetMaxY(self.navigationView.frame), kScreenWidth, kScreenHeight - self.navigationView.height);
    self.secondVC.view.frame = CGRectMake(kScreenWidth, CGRectGetMaxY(self.navigationView.frame), kScreenWidth, kScreenHeight - self.navigationView.height);
    
    //  默認,第一個視圖
    [self.view addSubview:self.fristVC.view];
    [self.view addSubview:self.secondVC.view];
    [self addChildViewController:self.fristVC];
-(void)swichTitleAction:(UISegmentedControl*)segmen{

 if ((self.currectVC == self.fristVC && segmen.selectedSegmentIndex ==0) || (self.currectVC == self.secondVC && segmen.selectedSegmentIndex == 1)) { 
      return;
 }

//--------ChildViewcontroller 間的切換--------------//
UIViewController* oldController = self.currectVC;
UIViewController*  newController = segmen.selectedSegmentIndex == 0? self.fristVC:self.secondVC;
 newController.view.frame = oldController.view.frame;
 [self addChildViewController:newController];

 [self transitionFromViewController:oldController toViewController:newController duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        
        if (finished) {
            
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.currectVC = newController;
        }else{
            
            self.currectVC = oldController;
        }
    }];
}

5, 快速實現(xiàn)圖片和View的模糊效果 FXBlurView -- https://github.com/nicklockwood/FXBlurView

6, 很多時候使用UIWebView 會導致內(nèi)存增加很大. 這個時候建議使用WKWebView,WKWebView 內(nèi)存占用非常小... WKWebView是iOS8 以后使用的, 然而Xcode8 都不支持iOS7了 還有什么理由 不用WKWebView代替UIWebView呢?

WKWebView的使用:

(1), 添加依賴庫: WebKit.framework

(2), 導入 #import <WebKit/WebKit.h>

(3),創(chuàng)建webview 跟之前的UIWebView 類似的

-(WKWebView *)webView{
    if (!_webView) {
        _webView = [[WKWebView alloc]initWithFrame:];
        _webView.navigationDelegate = self;
        _webView.scrollView.scrollEnabled = YES;
     
         [self.view addSubview:_webView];

        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
        [_webView loadRequest:request];
    }
    return _webView;
}

navigationDelegate 的代理方法

// 開始加載
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation;
// 加載完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation;
// 加載失敗
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;

更多WKWebView請點擊:http://nshipster.cn/wkwebkit/ http://www.reibang.com/p/403853b63537

7, 解決webview 中的文字亂碼 ! !

 dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
        NSString * htmlStr = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF8StringEncoding error:nil];
      
          [_webView loadHTMLString:htmlStr baseURL:[NSURL URLWithString:urlStr]]; 
 });

代替下面這種寫法:

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
   [_webView loadRequest:request];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市话瞧,隨后出現(xiàn)的幾起案子嫩与,更是在濱河造成了極大的恐慌,老刑警劉巖交排,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件划滋,死亡現(xiàn)場離奇詭異,居然都是意外死亡埃篓,警方通過查閱死者的電腦和手機处坪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來架专,“玉大人同窘,你說我怎么就攤上這事〔拷牛” “怎么了想邦?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長委刘。 經(jīng)常有香客問我丧没,道長鹰椒,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任呕童,我火速辦了婚禮漆际,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拉庵。我一直安慰自己灿椅,他們只是感情好,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布钞支。 她就那樣靜靜地躺著,像睡著了一般操刀。 火紅的嫁衣襯著肌膚如雪烁挟。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天骨坑,我揣著相機與錄音撼嗓,去河邊找鬼。 笑死欢唾,一個胖子當著我的面吹牛且警,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播礁遣,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼斑芜,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了祟霍?” 一聲冷哼從身側(cè)響起杏头,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沸呐,沒想到半個月后醇王,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡崭添,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年寓娩,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片呼渣。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡棘伴,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出徙邻,到底是詐尸還是另有隱情排嫌,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布缰犁,位于F島的核電站淳地,受9級特大地震影響怖糊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜颇象,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一伍伤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧遣钳,春花似錦扰魂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至倦淀,卻和暖如春蒋畜,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背撞叽。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工姻成, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人愿棋。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓科展,卻偏偏與公主長得像,于是被迫代替她去往敵國和親糠雨。 傳聞我的和親對象是個殘疾皇子才睹,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容