美文网首页
你不知道的小技巧

你不知道的小技巧

作者: 海耐射手 | 来源:发表于2022-05-14 17:33 被阅读0次

    一、iOS 加载网络图片不变形处理方法

    此方法用在tableView里加载速度还是可以的,没有卡顿现象

    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) {
       //图片家完成,回调里裁剪图片
       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 - 根据imgView的宽高获得图片的比例
    + (UIImage *)getImageFromUrl:(NSURL *)imgUrl imgViewWidth:(CGFloat)width imgViewHeight:(CGFloat)height{
        //根据网址将图片转化成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"都设置为动态参数${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,即当你安装该app到iPhone上显示的name。
    注意:Bundle Display name must correspond to Bundle name,即bundle display name和bundle name不能相差太远。例如bundle name设置为 TheApplication, 而 bundle display name设置为“金瓶梅”,则apple会拒绝你的app。
    当然,你也可以在info.plist file里修改这些属性。

    三、强制退出App

    有这样一个需求:不同意协议退出App
    可以认为制造Bug或采用:exit();、abort();、assert();、主动制造一个崩溃

    exit();

    1.附加了关闭打开文件与返回状态码给执行环境,并调用你用atexit注册的返回函数;
    2.警告:不要使用exit函数,调用exit会让用户感觉程序崩溃了,不会有按Home键返回时的平滑过渡和动画效果;
    3.另外,使用exit可能会丢失数据,因为调用exit并不会调用-applicationWillTerminate:方法和UIApplicationDelegate方法;
    exit(1);//是异常退出;
    exit(0);//是正常退出;

    abort();

    abort就像是点击了home键有过渡动画(我没发现效果)
    1.这是默认的程序结束函数,这种方式可能会或可能不会以刷新与关闭打开的文件
    或删除临时文件,这与你的设计有关。
    2.abort就像是点击了home键有过渡动画,使用的时建议选择abort();

    assert();

    1.assert(1)为oc中的宏,只在debug模式下有用,当条件成立时,程序不会终止掉;当条件不成立时,程序终止。
    2.oc程序中建议用assert(condition)函数,推荐使用assert宏;
    3.方法未实现完,放个ASSERT(0)调试运行时执行到此为报错中断,好知道成员函数还没写完。
    4.另一种情况是预防性的错误检查,在认为不可能的执行到的情况下加一句ASSERT(0),如果运行到此,代码逻辑或条件就可能有问题。
    assert(0);
    //作用是现计算表达式expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行
    assert(1);
    //为oc中的宏,只在debug模式下有用,当条件成立时,程序不会终止掉;当条件不成立时,程序终止。

    主动制造一个崩溃

    [[NSArray array] objectAtIndex:5];
    

    这种方式自然是不推荐的啦!如果你有崩溃日志收集功能则会产生误报,有正规途径还是走正规途径吧~~~~

    四、加载带H5标签的富文本

    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内容高度:
       CGSize size = CGSizeMake(KWIDTH - 30, MAXFLOAT);//设置高度宽度的最大限度
       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承载显示

       //设置代理 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 相机相册显示英文的解决办法

    info.plist里面添加 Localized resources can be mixed 设置为 YES(表示是否允许应用程序获取框架库内语言)

    相关文章

      网友评论

          本文标题:你不知道的小技巧

          本文链接:https://www.haomeiwen.com/subject/jzldurtx.html