iOS開發(fā)之圖文混排

前陣子工作不忙,正好自己又想要做圖片和文字的混編,雖然知道類似的軟件有很多扶认,但是因為平時幾乎只用原生的相機唇跨,所以對五花八門的相機也不了解狭吼,所以就想著自己來實現(xiàn)這個功能层坠。

這個功能的設計思路版本是:
1.最開始只打算加一張圖片和一段文字,所以就很死板刁笙,都是固定好的破花,沒什么新意;
2.后來想著一張圖片一段文字的固定格式疲吸,還是覺得不人性化座每,就想著任由使用者隨意添加圖片和文字,并且如果第一個添加的控件是textview磅氨,就默認文本居中尺栖,類似標題的存在;
3.第2個功能實現(xiàn)后烦租,有一天晚上編輯圖片給朋友的時候延赌,發(fā)現(xiàn)一旦添加好不想要了就得重新開頭做,然后就想著再添加個可以刪除imageview和textview的功能叉橱,并且imageview可以修改圖片挫以;
4.做完第三個功能后,覺得大致實現(xiàn)了窃祝,然后突然想到我可以再添加一個拖動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];
       //創(chuàng)建文本左對齊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];
        //創(chuàng)建文本居中對齊的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];
        //取消創(chuàng)建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) {
//如果創(chuàng)建的該textview是創(chuàng)建的第一個控件的布局
            textview.frame=CGRectMake(8, 8, [UIScreen mainScreen].bounds.size.width-16, 80);
            if (button.tag==1000) {
                textview.textAlignment=NSTextAlignmentLeft;
            }else{
                textview.textAlignment=NSTextAlignmentCenter;
            }
        }else{
//如果創(chuàng)建的該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");
}

}

好了 ,差不多就是這些了探膊,歡迎指教杠愧。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市逞壁,隨后出現(xiàn)的幾起案子流济,更是在濱河造成了極大的恐慌锐锣,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绳瘟,死亡現(xiàn)場離奇詭異雕憔,居然都是意外死亡,警方通過查閱死者的電腦和手機糖声,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門斤彼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人姨丈,你說我怎么就攤上這事畅卓∩醚” “怎么了蟋恬?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長趁冈。 經(jīng)常有香客問我歼争,道長,這世上最難降的妖魔是什么渗勘? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任沐绒,我火速辦了婚禮,結果婚禮上旺坠,老公的妹妹穿的比我還像新娘乔遮。我一直安慰自己,他們只是感情好取刃,可當我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布蹋肮。 她就那樣靜靜地躺著,像睡著了一般璧疗。 火紅的嫁衣襯著肌膚如雪坯辩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天崩侠,我揣著相機與錄音漆魔,去河邊找鬼。 笑死却音,一個胖子當著我的面吹牛改抡,可吹牛的內容都是我干的。 我是一名探鬼主播系瓢,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼阿纤,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了八拱?” 一聲冷哼從身側響起阵赠,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤涯塔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后清蚀,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體匕荸,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年枷邪,在試婚紗的時候發(fā)現(xiàn)自己被綠了榛搔。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡东揣,死狀恐怖践惑,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情嘶卧,我是刑警寧澤尔觉,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站芥吟,受9級特大地震影響侦铜,放射性物質發(fā)生泄漏。R本人自食惡果不足惜钟鸵,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一钉稍、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧棺耍,春花似錦贡未、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至左敌,卻和暖如春瘾蛋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背矫限。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工哺哼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叼风。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓取董,卻偏偏與公主長得像,于是被迫代替她去往敵國和親无宿。 傳聞我的和親對象是個殘疾皇子茵汰,可洞房花燭夜當晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內容