IOS開發隨筆

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

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

-(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;
}

如果內存還是太大的話 可以這樣設置SD

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

2, 用TTTAttributedLabel 給label中關鍵詞加點擊事件

屏幕快照 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年清晨哪位和我視頻的女網友:夜未央。那海藻般的頭發,白色的棉布內衣,如同安妮寶貝筆下那個,慵懶,干凈,純凈的女子一般。當她對我微微一笑,我即刻頭暈目眩,感覺生命仿佛被一道白光刺穿,涉世未深的我第一次感覺到了成熟女人的魅力,依依不舍的關掉視頻后,我昏睡了整整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];
    
    //正常狀態下的屬性
    _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 實現防淘寶商品詳情中查看寶貝圖文詳情方式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, 快速實現圖片和View的模糊效果 FXBlurView -- https://github.com/nicklockwood/FXBlurView

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

WKWebView的使用:

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

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

(3),創建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.lxweimin.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];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容