美文网首页
iOS 小知识2

iOS 小知识2

作者: AppleTTT | 来源:发表于2018-04-08 09:30 被阅读28次

    iOS 小知识2

    隐藏导航栏的正确姿势

    方法一

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
    
    

    方法二(契合左滑返回手势)

    - (void)viewDidLoad {
        [super viewDidLoad];
        // 设置导航控制器的代理为self
        self.navigationController.delegate = self;
    }
    
    #pragma mark - UINavigationControllerDelegate
    // 将要显示控制器
    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
        // 判断要显示的控制器是否是自己
        BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
        [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
    }
    

    iOS 生成PDF

    生成PDF文件步骤:

    1创建PDF上下文
    //1>.获取沙盒路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    //2>.拼接路径
    NSString *PDFPath = [path stringByAppendingPathComponent:@"123.pdf"];
    //3>.创建PDF上下文
    UIGraphicsBeginPDFContextToFile(PDFPath, CGRectZero, NULL);
    2.创建PDF页面
    1.初始化image,画image;
    for (int i = 0; i < 6; i++) {
    if (i % 2 == 0) {
    //创建PDF页
    UIGraphicsBeginPDFPage();
    }
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"NatGeo%02d",i+1]];
    [image drawInRect:CGRectMake(0, (i % 2) * h, w, h)];
    }
    2.1添加文字水印
    // 添加文字
    NSString *text = @"www.baidu.com";
    
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:18],
    NSParagraphStyleAttributeName: paragraph,
    NSForegroundColorAttributeName: [UIColor whiteColor]};
    
    [text drawInRect:CGRectMake(0, (i % 2) * h + h - 40, w, 40) withAttributes:dict];
    3.关闭上下文
    UIGraphicsEndPDFContext();
    二、渐变
    1>线性渐变
    //1.获得上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //绘制渐性渐变
    //创建色彩空间
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    //颜色组件,每四个一组
    CGFloat components[] = {1.0,0.0,0.0,1.0,1.0,1.0,1.0,1.0};
    //开始和结束的位置数组,以百分比来表示的
    CGFloat location[] = {0.0,1.0};
    //创建渐变
    CGGradientRef gradinet = CGGradientCreateWithColorComponents(space,components, location, 2);
    //裁剪
    // CGContextClipToRect(context, CGRectMake(0, 20, 100, 100));
    //开始绘制
    CGContextDrawLinearGradient(context, gradinet, CGPointMake(0, 0), CGPointMake(320, 460), kCGGradientDrawsAfterEndLocation);
    //释放内容
    CGGradientRelease(gradinet);
    CGColorSpaceRelease(space);
    2>绘制径向渐变
    CGContextDrawRadialGradient(context, gradinet, CGPointMake(200, 240), 80, CGPointMake(160, 300), 200, kCGGradientDrawsBeforeStartLocation);
    其他的步骤和线性渐变都一样。
    

    用此种方式生成的 PDF 文件的大小主要看 PDF 中图片的大小,我一开始用的时候,生成的 PDF 一直很大,后来发现是图片本身就很大了,在压缩图片之后,达到了十张图片才不到1M,而且图片在电脑上查看依然比较清晰;
    压缩图片可以使用 UIImageJPEGRepresentation 或者是压缩 size
    参考1
    参考2

    autolayout动画

     [self.view layoutIfNeeded]; 
        [UIView animateWithDuration:1.0 animations:^{
            [self updateConstraintsForMode]; 
            [self.view layoutIfNeeded];
        }];
    

    for循环导致的内存暴涨

    在我的一个项目中,需要通过 for 循环来将用户选中或者拍照的图片全部经过复杂的处理,这过程需要消耗大量的 CPUmemorymemory 会一直递涨),当选中的照片超过60张的时候(iPhone6 Plus),APP 就会崩溃了,而老板的需要时至少100张,进过查阅后,加一个自动释放池就可以了,如下:

    for (NSInteger i = 0; i < self.data.count; i++) {
        @autoreleasepool {
            //以下为非常耗资源的操作
            ScanModel *model = self.data[i];
            UIImage *image = [UIImage imageWithData: model.originData];
            [cropImageView setOriginImage: image];
            UIImage *cropImage = [cropImageView cropPhoto];
            //压缩剪裁后的图片
            cropImage = [cropImage see_compressImageWithMaxSize: CGSizeMake(cropImage.size.width / 2, cropImage.size.height / 2)];
            NSData *imageData = UIImageJPEGRepresentation(cropImage, 0.5);
            cropImage = [UIImage imageWithData: imageData];
            
            model.cropImage = cropImage;
            hud.labelText = [NSString stringWithFormat: @"%zd/%zd", i + 1, self.data.count];
        //                sleep(1);
        }
        }
    

    collectionView的headerView

    1、在UICollectionViewFlowLayout中headerReferenceSize设置headView的大小
    2、注册headerView:
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    3、设置headerView的样式:
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionReusableView *reusableView = nil;
        if (kind == UICollectionElementKindSectionHeader) {
            UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
            reusableView = header;
        }
        reusableView.backgroundColor = [UIColor greenColor];
        if (kind == UICollectionElementKindSectionFooter)
        {
            UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
            footerview.backgroundColor = [UIColor purpleColor];
            reusableView = footerview;
        }
        return reusableView;
    }
    
    

    旋转动画

    - (IBAction)rotateImage:(UIButton *)sender {
        CGFloat value = (int)floorf((self.rotateSlider + 1)*2) + 1;
        if(value>4){ value -= 4; }
        self.rotateSlider = value / 2 - 1;
        [UIView animateWithDuration:0.5 animations:^{
            [self rotateStateDidChange];
        }];
        
        self.model.cropImage = [UIImage getRotationImageWithImage: self.model.cropImage Angle: self.angle];
    }
    
    #pragma mark - 旋转动画操作
    - (CATransform3D)rotateTransform:(CATransform3D)initialTransform clockwise:(BOOL)clockwise
    {
        CGFloat arg = self.rotateSlider*M_PI;
        if(!clockwise){
            arg *= -1;
        }
        
        CATransform3D transform = initialTransform;
        transform = CATransform3DRotate(transform, arg, 0, 0, 1);
        transform = CATransform3DRotate(transform, 0*M_PI, 0, 1, 0);
        transform = CATransform3DRotate(transform, 0*M_PI, 1, 0, 0);
        
        return transform;
    }
    - (void)rotateStateDidChange{
        CATransform3D transform = [self rotateTransform:CATransform3DIdentity clockwise:YES];
    
        CGFloat arg = self.rotateSlider*M_PI;
        CGFloat Wnew = fabs(self.imageView.frame.size.width * cos(arg)) + fabs(self.imageView.frame.size.height * sin(arg));
        CGFloat Hnew = fabs(self.imageView.frame.size.width * sin(arg)) + fabs(self.imageView.frame.size.height * cos(arg));
        
        CGFloat Rw = self.imageView.frame.size.width / Wnew;
        CGFloat Rh = self.imageView.frame.size.height / Hnew;
        CGFloat scale;
        //为了在旋转的过程中和旋转结束后看到完整的图像做的处理
        if (self.imageView.bounds.size.width < self.imageView.bounds.size.height) {
            scale = MIN(Rw, Rh) * 1;
        }else{
            scale = MAX(Rw, Rh) * 1;
        }
        
        transform = CATransform3DScale(transform, scale, scale, 1);
        self.imageView.layer.transform = transform;
        
    }
    

    获取旋转之后的图片

    /**
     *  得到旋转后的图片
     *
     *  @param image 原图
     *  @param Angle 角度(0~360)
     *
     *  @return 新生成的图片
     */
    +(UIImage  *)getRotationImageWithImage:(UIImage *)image Angle:(CGFloat)Angle
    {
        
        UIView *RootBackView = [[UIView alloc] initWithFrame:CGRectMake(0,0, image.size.width, image.size.height)];
        CGAffineTransform t = CGAffineTransformMakeRotation( Angle* M_PI / 180);
        RootBackView.transform = t;
        CGSize rotatedSize = RootBackView.frame.size;
        UIGraphicsBeginImageContext(rotatedSize);
        CGContextRef theContext = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(theContext, rotatedSize.width/2, rotatedSize.height/2);
        CGContextRotateCTM(theContext, Angle * M_PI / 180);
        CGContextScaleCTM(theContext, 1.0, -1.0);
        CGContextDrawImage(theContext,
                           CGRectMake(-image.size.width / 2,
                                      -image.size.height / 2,
                                      image.size.width,
                                      image.size.height),
                           [image CGImage]);
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return newImage;
    }
    

    StoryBoard & Xib拖线错误

    今天在给 IBOutlet 拖线的时候报出如下错误:
    could not insert new outlet connection
    解决办法:将此控制器或者 View 对应的 .h .m .swift 文件删除 (Remove Reference),然后到对应文件夹中重新添加即可

    相关文章

      网友评论

          本文标题:iOS 小知识2

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