iOS开发之图文混排

前阵子工作不忙,正好自己又想要做图片和文字的混编,虽然知道类似的软件有很多,但是因为平时几乎只用原生的相机,所以对五花八门的相机也不了解,所以就想着自己来实现这个功能。

这个功能的设计思路版本是:
1.最开始只打算加一张图片和一段文字,所以就很死板,都是固定好的,没什么新意;
2.后来想着一张图片一段文字的固定格式,还是觉得不人性化,就想着任由使用者随意添加图片和文字,并且如果第一个添加的控件是textview,就默认文本居中,类似标题的存在;
3.第2个功能实现后,有一天晚上编辑图片给朋友的时候,发现一旦添加好不想要了就得重新开头做,然后就想着再添加个可以删除imageview和textview的功能,并且imageview可以修改图片;
4.做完第三个功能后,觉得大致实现了,然后突然想到我可以再添加一个拖动textview改变页面布局的功能;
5.增加了添加输入框前先选择文本对齐方式的功能;
6.完成。

最终效果预览

点击此处预览

textview相关

textview的相关操作gif图
//文本按钮
-(UIButton *)textButton{
    if (_textButton==nil) {
        _textButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [_textButton setImage:[UIImage imageNamed:@"text"] forState:UIControlStateNormal];
        _textButton.layer.cornerRadius=25;
        _textButton.layer.masksToBounds=YES;
        _textButton.backgroundColor=[UIColor whiteColor];
        [_textButton addTarget:self action:@selector(textButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _textButton;
}
//文本按钮事件
-(void)textButtonAction{
    self.chooseAlignmentView.hidden=NO; 
}
//选择文本对齐方式view
-(UIView *)chooseAlignmentView{
    if (_chooseAlignmentView==nil) {
        _chooseAlignmentView=[[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-145, self.textButton.frame.origin.y, 170, 50)];
        _chooseAlignmentView.layer.cornerRadius=25;
        _chooseAlignmentView.layer.masksToBounds=YES;
        _chooseAlignmentView.layer.borderColor=[[UIColor whiteColor] CGColor];
        _chooseAlignmentView.layer.borderWidth=2;
        _chooseAlignmentView.backgroundColor=[UIColor colorWithRed:253/255.0 green:163/255.0 blue:155/255.0 alpha:1];
       //创建文本左对齐textview
        UIButton *leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.backgroundColor=[UIColor colorWithRed:253/255.0 green:163/255.0 blue:155/255.0 alpha:1];
        [leftButton setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
        leftButton.layer.cornerRadius=20;
        leftButton.layer.masksToBounds=YES;
        leftButton.tag=1000;
        [leftButton addTarget:self action:@selector(alignButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //创建文本居中对齐的textview
        UIButton *centerButton=[UIButton buttonWithType:UIButtonTypeCustom];
        centerButton.layer.cornerRadius=20;
        centerButton.layer.masksToBounds=YES;
        centerButton.backgroundColor=[UIColor colorWithRed:253/255.0 green:163/255.0 blue:155/255.0 alpha:1];
        [centerButton setImage:[UIImage imageNamed:@"center"] forState:UIControlStateNormal];
        centerButton.tag=2000;
        [centerButton addTarget:self action:@selector(alignButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //取消创建textview
        UIButton *cancelButton=[UIButton buttonWithType:UIButtonTypeCustom];
        cancelButton.layer.cornerRadius=20;
        cancelButton.layer.masksToBounds=YES;
        cancelButton.backgroundColor=[UIColor colorWithRed:253/255.0 green:163/255.0 blue:155/255.0 alpha:1];
        [cancelButton setImage:[UIImage imageNamed:@"cancel1"] forState:UIControlStateNormal];
        cancelButton.tag=3000;
        [cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];

        leftButton.frame=CGRectMake(25, 5, 40, 40);
        [_chooseAlignmentView addSubview:leftButton];
        centerButton.frame=CGRectMake(65, 5, 40, 40);
        [_chooseAlignmentView addSubview:centerButton];
        cancelButton.frame=CGRectMake(105, 5, 40, 40);
        [_chooseAlignmentView addSubview:cancelButton];

        _chooseAlignmentView.hidden=YES;
    }
    return _chooseAlignmentView;
}

-(void)cancelButtonAction{
    self.chooseAlignmentView.hidden=YES;
}

-(void)alignButtonAction:(UIButton *)button{
        UITextView * textview=[UITextView new];
        textview.layer.borderColor=[UIColor redColor].CGColor;
        textview.layer.borderWidth=2;
        textview.delegate=self;
        textview.text=@"";
        textview.backgroundColor=[UIColor clearColor];
        textview.font=[UIFont fontWithName:@"FZJLJW--GB1-0" size:26];
        self.toolBarView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
        textview.inputAccessoryView=self.toolBarView;
    //给textview添加长按手势
        UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];
        [textview addGestureRecognizer:longPress];
    //给textview添加拖拽手势
        UIPanGestureRecognizer *ges=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
        [textview addGestureRecognizer:ges];

        if (self.totalViewsCount==0) {
//如果创建的该textview是创建的第一个控件的布局
            textview.frame=CGRectMake(8, 8, [UIScreen mainScreen].bounds.size.width-16, 80);
            if (button.tag==1000) {
                textview.textAlignment=NSTextAlignmentLeft;
            }else{
                textview.textAlignment=NSTextAlignmentCenter;
            }
        }else{
//如果创建的该textview不是第一个控件的布局,lastview记录的是该页面的最后一个控件
            textview.frame=CGRectMake(8, self.lastView.frame.origin.y+self.lastView.frame.size.height+8, [UIScreen mainScreen].bounds.size.width-16, 80);
            if (button.tag==1000) {
                textview.textAlignment=NSTextAlignmentLeft;
            }else{
                textview.textAlignment=NSTextAlignmentCenter;
            }
        }

        [self cellAddSubview:textview];

    self.chooseAlignmentView.hidden=YES;

}

-(void)cellAddSubview:(UIView *)view{

    UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

    [self.totalViewArr addObject:view];
    self.lastView=view;
    self.totalViewsCount++;
    view.tag=930+self.totalViewArr.count;

    [cell.contentView addSubview:view];
    self.cellHeight=self.lastView.frame.origin.y+self.lastView.frame.size.height+13;
    [self.tableview reloadData];
}
//长按textview选择是否删除
-(void)longTap:(UITapGestureRecognizer *)ges{

    UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"确定删除吗" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        self.changedTextview=(UITextView *)ges.view;
        //从self.totalViewArr中删除需要删除的textview
        NSMutableArray *tmp=[NSMutableArray array];
        for (UIView *view in self.totalViewArr) {
            if (view.tag==self.changedTextview.tag) {

            }else{
                [tmp addObject:view];
            }
        }
        self.totalViewArr=[NSMutableArray arrayWithArray:tmp];
//给self.totalViewArr中的控件重新赋tag值
        for (UIView *view in self.totalViewArr) {
            NSInteger index=[self.totalViewArr indexOfObject:view];
            view.tag=931+index;
        }

        [self reLayoutSubviewsInCell:self.changedTextview];

    }]];

    [self presentViewController:alertVC animated:YES completion:^{

    }];
}
//重新布局tableviewcell中的子控件
-(void)reLayoutSubviewsInCell:(UIView *)changedView{

    UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

    NSArray *views=cell.contentView.subviews;

    for (int i=0; i<views.count; i++) {
        UIView *view=views[i];
        if (i==changedView.tag-931) {
            [view removeFromSuperview];
        }
    }

    for (int i=0; i<self.totalViewArr.count; i++) {

        UIView *view=self.totalViewArr[i];

        if (i<changedView.tag-931) {

        }else{

            CGRect rect=view.frame;
            view.frame=CGRectMake(rect.origin.x, rect.origin.y-changedView.frame.size.height-8, rect.size.width, rect.size.height);
        }

    }

    self.lastView=[self.totalViewArr lastObject];

    self.cellHeight=self.lastView.frame.origin.y+self.lastView.frame.size.height+13;
    [self.tableview reloadData];

}
//textview的拖拽手势
-(void)panAction:(UIPanGestureRecognizer *)ges{

    UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    UIView *gesview=ges.view;
    CGPoint point=[ges translationInView:cell.contentView];
    if (ges.state==UIGestureRecognizerStateEnded) {
        if (point.y>=0) {
            NSMutableArray *tmp=[NSMutableArray array];
            //往下
            for (int i=(int)gesview.tag-930; i<self.totalViewArr.count; i++) {
                UIView *view=self.totalViewArr[i];
                if (i==self.totalViewArr.count-1) {
                    if (gesview.center.y+point.y>view.center.y ) {
                       //ok
                        if (self.totalViewArr.count==2) {
                            UIView *last=[self.totalViewArr lastObject];
                            CGRect rect=last.frame;
                            last.frame=CGRectMake(rect.origin.x,gesview.frame.origin.y ,rect.size.width , rect.size.height);
                            gesview.frame=CGRectMake(gesview.frame.origin.x, last.frame.size.height+last.frame.origin.y+8, gesview.frame.size.width, gesview.frame.size.height);
                            [tmp addObject:last];
                            [tmp addObject:gesview];
                            [self breakFor:tmp];
                            break;
                        }else{
                            //ok
                            if (gesview.tag==931) {
                                for (int j=1; j<self.totalViewArr.count; j++) {
                                    UIView *view=self.totalViewArr[j];
                                    CGRect rect=view.frame;
                                    view.frame=CGRectMake(rect.origin.x, rect.origin.y-8-gesview.frame.size.height, rect.size.width, rect.size.height);
                                    [tmp addObject:view];
                                }
                                UIView *last=tmp[self.totalViewArr.count-2];
                                gesview.frame=CGRectMake(gesview.frame.origin.x, last.frame.size.height+8+last.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
                                [tmp addObject:gesview];
                                [self breakFor:tmp];
                                break;
                            //ok
                            }else{
                                for (int j=0; j<gesview.tag-931; j++) {
                                    UIView *view=self.totalViewArr[j];
                                    [tmp addObject:view];
                                }
                                for (int j=(int)gesview.tag-930; j<self.totalViewArr.count; j++) {
                                    UIView *view=self.totalViewArr[j];
                                    CGRect rect=view.frame;
                                    view.frame=CGRectMake(rect.origin.x, rect.origin.y-8-gesview.frame.size.height, rect.size.width, rect.size.height);
                                    [tmp addObject:view];
                                }
                                UIView *last=[tmp lastObject];
                                gesview.frame=CGRectMake(gesview.frame.origin.x, last.frame.origin.y+last.frame.size.height+8, gesview.frame.size.width, gesview.frame.size.height);
                                [tmp addObject:gesview];
                                [self breakFor:tmp];
                                break;
                            }
                        }
                    }
                }else{
                    UIView *nextView=self.totalViewArr[i+1];
                    if (gesview.center.y+point.y>view.center.y && gesview.center.y+point.y<nextView.center.y) {
                        //ok
                        if (gesview.tag==931) {
                            for (int j=1; j<i+1; j++) {
                                UIView *view=self.totalViewArr[j];
                                CGRect rect=view.frame;
                                view.frame=CGRectMake(rect.origin.x, rect.origin.y-8-gesview.frame.size.height, rect.size.width, rect.size.height);
                                [tmp addObject:view];
                            }
                            UIView *pre=self.totalViewArr[i];
                            gesview.frame=CGRectMake(gesview.frame.origin.x, pre.frame.origin.y+pre.frame.size.height+8, gesview.frame.size.width, gesview.frame.size.height);
                            [tmp addObject:gesview];
                            for (int j=i+1; j<self.totalViewArr.count; j++) {
                                UIView *view=self.totalViewArr[j];
                                [tmp addObject:view];
                            }
                            [self breakFor:tmp];
                            break;
                        //ok
                        }else{
                            for (int j=0; j<gesview.tag-931; j++) {
                                [tmp addObject:self.totalViewArr[j]];
                            }
                            for (int j=(int)gesview.tag-930; j<i+1; j++) {
                                UIView *tmpview=self.totalViewArr[j];
                                CGRect rect=tmpview.frame;
                                tmpview.frame=CGRectMake(rect.origin.x, rect.origin.y-8-gesview.frame.size.height, rect.size.width, rect.size.height);
                                [tmp addObject:tmpview];
                            }
                            UIView *last=[tmp lastObject];
                            gesview.frame=CGRectMake(gesview.frame.origin.x, last.frame.origin.y+last.frame.size.height+8, gesview.frame.size.width, gesview.frame.size.height);
                            [tmp addObject:gesview];

                            for (int j=i+1; j<self.totalViewArr.count; j++) {
                                [tmp addObject:self.totalViewArr[j]];
                            }

                            [self breakFor:tmp];
                            break;
                        }
                    }
                }
            }
        }else{

            NSMutableArray *tmp=[NSMutableArray array];

            for (int i=0; i<gesview.tag-931; i++) {

                UIView *view=self.totalViewArr[i];
                if (gesview.center.y+point.y<view.center.y) {
                    if (self.totalViewArr.count==2) {
                        gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
                        [tmp addObject:gesview];
                        view.frame=CGRectMake(view.frame.origin.x, gesview.frame.origin.y+gesview.frame.size.height+8, view.frame.size.width, view.frame.size.height);
                        [tmp addObject:view];
                        [self breakFor:tmp];
                        break;
                    }else{
                        if (view.tag==931&&gesview.tag==930+self.totalViewArr.count) {
                            gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
                            [tmp addObject:gesview];
                            for (int j=0; j<self.totalViewArr.count-1; j++) {
                                UIView *viewtmp=self.totalViewArr[j];
                                viewtmp.frame=CGRectMake(viewtmp.frame.origin.x, viewtmp.frame.origin.y+gesview.frame.size.height+8, viewtmp.frame.size.width, viewtmp.frame.size.height);
                                [tmp addObject:viewtmp];
                            }
                            [self breakFor:tmp];
                            break;
                        }else if (gesview.tag==930+self.totalViewArr.count && view.tag!=931){
                            for (int j=0; j<view.tag-931; j++) {
                                UIView *tmpview=self.totalViewArr[j];
                                [tmp addObject:tmpview];
                            }
                            gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
                            [tmp addObject:gesview];
                            for (int j=(int)view.tag-931; j<self.totalViewArr.count-1; j++) {
                                UIView *tmpview=self.totalViewArr[j];
                                CGRect rect=tmpview.frame;
                                tmpview.frame=CGRectMake(rect.origin.x,gesview.frame.size.height+8+rect.origin.y, rect.size.width, rect.size.height);
                                [tmp addObject:tmpview];
                            }
                            [self breakFor:tmp];
                            break;
                        }else if (view.tag==931 && gesview.tag!=self.totalViewArr.count+930){
                            gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
                            [tmp addObject:gesview];
                            for (int j=0; j<gesview.tag-931; j++) {
                                UIView *tmpview=self.totalViewArr[j];
                                CGRect rect=tmpview.frame;
                                tmpview.frame=CGRectMake(rect.origin.x, rect.origin.y+gesview.frame.size.height+8, rect.size.width, rect.size.height);
                                [tmp addObject:tmpview];
                            }
                            for (int j=(int)gesview.tag-930; j<self.totalViewArr.count; j++) {
                                UIView *tmpview=self.totalViewArr[j];
                                [tmp addObject:tmpview];
                            }
                            [self breakFor:tmp];
                            break;
                        }else if (view.tag!=931 && gesview.tag!=self.totalViewArr.count+930){
                            for (int j=0; j<i; j++) {
                                UIView *tmpview=self.totalViewArr[j];
                                [tmp addObject:tmpview];
                            }
                            gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
                            [tmp addObject:gesview];
                            for (int j=i; j<gesview.tag-931; j++) {
                                UIView *tmpview=self.totalViewArr[j];
                                CGRect rect=tmpview.frame;
                                tmpview.frame=CGRectMake(rect.origin.x,rect.origin.y+gesview.frame.size.height+8 , rect.size.width, rect.size.height);
                                [tmp addObject:tmpview];
                            }
                            for (int j=(int)gesview.tag-930; j<self.totalViewArr.count; j++) {
                                UIView *tmpview=self.totalViewArr[j];
                                [tmp addObject:tmpview];
                            }
                            [self breakFor:tmp];
                            break;
                        }
                    }
                }
            }
        }
    }
}

-(void)breakFor:(NSMutableArray *)tmp{

    UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    self.totalViewArr=[NSMutableArray arrayWithArray:tmp];
    for (UIView *view in self.totalViewArr) {
        [view removeFromSuperview];
        NSInteger index=[self.totalViewArr indexOfObject:view];
        view.tag=931+index;
    }
    self.lastView=[self.totalViewArr lastObject];
    for (UIView *view in self.totalViewArr) {
        [cell.contentView addSubview:view];
    }
    [self.tableview reloadData];

}

imageview相关

-(UIButton *)pictureButton{

if (_pictureButton==nil) {
    _pictureButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_pictureButton setImage:[UIImage imageNamed:@"picture"] forState:UIControlStateNormal];
    _pictureButton.layer.cornerRadius=25;
    _pictureButton.layer.masksToBounds=YES;
    _pictureButton.backgroundColor=[UIColor whiteColor];
    [_pictureButton addTarget:self action:@selector(pictureButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _pictureButton;
} 
//点击图片按钮选择拍照还是相册 
-(void)pictureButtonAction{

UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

[alertVC addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    UIImagePickerController *vc=[[UIImagePickerController alloc]init];
    vc.delegate=self;
    vc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    vc.allowsEditing=YES;
    vc.videoQuality=UIImagePickerControllerQualityTypeHigh;

    [self presentViewController:vc animated:YES completion:^{

    }];

}]];

[alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    UIImagePickerController *vc=[[UIImagePickerController alloc]init];
    vc.delegate=self;
    vc.sourceType=UIImagePickerControllerSourceTypeCamera;
    vc.allowsEditing=YES;
    vc.videoQuality=UIImagePickerControllerQualityTypeHigh;

    [self presentViewController:vc animated:YES completion:^{

    }];

}]];

[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}]];

[self presentViewController:alertVC animated:YES completion:^{

}];
} 

//图片选择完成后的操作 

-(void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info{

NSLog(@"%@",info);
UIImage *img=[info objectForKey:@"UIImagePickerControllerEditedImage"];
//UIImage *img=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGSize size=img.size;

if (self.createOrNot==YES) {
    UIImageView * imageview1=[UIImageView new];
    imageview1.layer.cornerRadius=10;
    imageview1.layer.masksToBounds=YES;
    imageview1.layer.borderColor=[UIColor whiteColor].CGColor;
    imageview1.layer.borderWidth=6;
    imageview1.userInteractionEnabled=YES;
    imageview1.image=img;
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(change:)];
    [imageview1 addGestureRecognizer:tap];

    if (self.totalViewsCount==0) {
        if (size.width>[UIScreen mainScreen].bounds.size.width) {
            imageview1.frame=CGRectMake(6, 8, [UIScreen mainScreen].bounds.size.width-12, size.height*([UIScreen mainScreen].bounds.size.width-12)/size.width);
        }else{
            imageview1.frame=CGRectMake([UIScreen mainScreen].bounds.size.width/2.0-size.width/2.0, 8,size.width, size.height);
        }

    }else{
        if (size.width>[UIScreen mainScreen].bounds.size.width) {
            imageview1.frame=CGRectMake(6, self.lastView.frame.origin.y+self.lastView.frame.size.height+8, [UIScreen mainScreen].bounds.size.width-12, size.height*([UIScreen mainScreen].bounds.size.width-12)/size.width);
        }else{
            imageview1.frame=CGRectMake([UIScreen mainScreen].bounds.size.width/2.0-size.width/2.0, self.lastView.frame.origin.y+self.lastView.frame.size.height+8,size.width, size.height);
        }

    }

    [self cellAddSubview:imageview1];

}else{
    self.needChangedImageview.image=img;
    self.createOrNot=YES;

}

[picker dismissViewControllerAnimated:YES completion:^{

}];
} 

//点击图片进行修改或删除 

-(void)change:(UITapGestureRecognizer *)ges{

UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertVC addAction:[UIAlertAction actionWithTitle:@"改变图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    self.createOrNot=NO;
    self.needChangedImageview=(UIImageView *)ges.view;
    [self pictureButtonAction];
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"删除图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    UIImageView *imageview=(UIImageView *)ges.view;
    self.changedImageView=imageview;

    NSMutableArray *tmp=[NSMutableArray array];
    for (UIView *view in self.totalViewArr) {
        if (view.tag==imageview.tag) {

        }else{
            [tmp addObject:view];
        }
    }
    self.totalViewArr=[NSMutableArray arrayWithArray:tmp];
    for (UIView *view in self.totalViewArr) {
        NSInteger index=[self.totalViewArr indexOfObject:view];
        view.tag=931+index;
    }

    [self reLayoutSubviewsInCell:self.changedImageView];

}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}]];

[self presentViewController:alertVC animated:YES completion:^{

}];
}

tableview相关

-(UITableView *)tableview{

if (_tableview==nil) {
    _tableview=[[UITableView alloc]init];
    _tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableview.delegate=self;
    _tableview.dataSource=self;
}
return _tableview;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 1;
}

-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellid=@"cellid";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];

if (cell==nil) {
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor colorWithRed:arc4random()%6/255.0 green:arc4random()%6/255.0 blue:arc4random()%6/255.0 alpha:1];
    self.tableview.backgroundColor=cell.backgroundColor;

}

return cell;
}

-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{

return self.cellHeight;
}

随机改变背景颜色

-(UIButton *)colorButton{

if (_colorButton==nil) {
    _colorButton=[UIButton buttonWithType:UIButtonTypeCustom];
    _colorButton.backgroundColor=[UIColor whiteColor];
    [_colorButton setImage:[UIImage imageNamed:@"color"] forState:UIControlStateNormal];
    _colorButton.layer.cornerRadius=25;
    _colorButton.layer.masksToBounds=YES;
    [_colorButton addTarget:self action:@selector(colorButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _colorButton;
}
//随机改变背景颜色
-(void)colorButtonAction{

UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.backgroundColor=[UIColor colorWithRed:arc4random()%6/255.0 green:arc4random()%6/255.0 blue:arc4random()%6/255.0 alpha:1];
self.tableview.backgroundColor=cell.backgroundColor;
}

保存图片到手机

-(UIButton *)saveButton{

if (_saveButton==nil) {
    _saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
    _saveButton.backgroundColor=[UIColor whiteColor];
    [_saveButton setImage:[UIImage imageNamed:@"finish"] forState:UIControlStateNormal];
    _saveButton.layer.cornerRadius=25;
    _saveButton.layer.masksToBounds=YES;
    [_saveButton addTarget:self action:@selector(saveButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _saveButton;
}

-(void)saveButtonAction{

UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

for (UIView *view in [cell.contentView subviews]) {
    if ([view isKindOfClass:[UITextView class]]) {
        UITextView *textview=(UITextView *)view;
        textview.layer.borderWidth=0;
    }
}
UIImageWriteToSavedPhotosAlbum([self convertViewToImage:cell], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
} 

//view转为image 

-(UIImage )convertViewToImage:(UIView )v{

CGSize s=v.bounds.size;
UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
[v.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
} 

//保存结束后的回调
-(void)image:(UIImage )image didFinishSavingWithError:(NSError )error contextInfo:(void *)contextInfo{

if(error == nil) {

NSLog(@"success");
}else{

NSLog(@"fail");
}

}

好了 ,差不多就是这些了,欢迎指教。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容