登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

朋友圈(文字+图片)

[复制链接]
来自: Hera 分类: iOS精品源码 上传时间: 2016-8-2 08:56:44
Tag:微信朋友圈 图片 相册 选择器 wechat

项目介绍:

1.类似发朋友圈,文字+图片
2.图片最多选9张,并可删除
3.图片可换行

集成代码:
[Objective-C] 查看源文件 复制代码
#import "ViewController.h"
#import "ZYQAssetPickerController.h"

#define ScreenHeight    [[UIScreen mainScreen] bounds].size.height
#define ScreenWidth     [[UIScreen mainScreen] bounds].size.width

@interface ViewController ()<ZYQAssetPickerControllerDelegate,UINavigationControllerDelegate,UITextViewDelegate>
{
    UIView               *_editv;
    UITextView           *_textView;
    UILabel              *_placeholderLabel;
    UIButton             *_addPic;
    NSMutableArray       *_imageArray;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _imageArray = [NSMutableArray array];
    
    // 评论 + 照片
    _editv = [[UIView alloc] initWithFrame:CGRectMake(15, 50, ScreenWidth-15*2, 0)];
    _editv.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_editv];
    
    // 评论 UITextView
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 15, CGRectGetWidth(_editv.frame)-15*2, 70)];
    _textView.backgroundColor = [UIColor redColor];
    _textView.delegate = self;
    [_editv addSubview:_textView];
    
    // 提示字
    _placeholderLabel = [[UILabel alloc] init];
    _placeholderLabel.frame =CGRectMake(5, 6, CGRectGetWidth(_editv.frame)-15*4, 20);
    _placeholderLabel.text = @"在此编辑您的而分享内容!";
    _placeholderLabel.textColor = [UIColor colorWithWhite:0.4 alpha:0.6];
    _placeholderLabel.font = [UIFont systemFontOfSize:12.0];
    _placeholderLabel.enabled = NO; // lable必须设置为不可用
    _placeholderLabel.backgroundColor = [UIColor clearColor];
    [_textView addSubview:_placeholderLabel];
    
    // + pic
    _addPic = [UIButton buttonWithType:UIButtonTypeCustom];
    _addPic.frame = CGRectMake(15, CGRectGetMaxY(_textView.frame)+15, 70, 70);
    [_addPic setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [_addPic addTarget:self action:@selector(addPicEvent) forControlEvents:UIControlEventTouchUpInside];
    [_editv addSubview:_addPic];
    _editv.frame = CGRectMake(15, 50, ScreenWidth-15*2, CGRectGetMaxY(_addPic.frame)+20);
}

#pragma mark - UIbutton event

- (void)addPicEvent
{
    if (_imageArray.count >= 9) {
        NSLog(@"最多只能上传9张图片");
    } else {
        [self selectPictures];
    }
}

// 本地相册选择多张照片
- (void)selectPictures
{
    ZYQAssetPickerController *picker = [[ZYQAssetPickerController alloc] init];
    picker.maximumNumberOfSelection = 9-_imageArray.count;
    picker.assetsFilter = [ALAssetsFilter allPhotos];
    picker.showEmptyGroups = NO;
    picker.delegate = self;
    picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings)
                              {
                                  if ([[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo])
                                  {
                                      NSTimeInterval duration = [[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
                                      
                                      return duration >= 5;
                                  } else {
                                      return YES;
                                  }
                              }];
    
    [self presentViewController:picker animated:YES completion:NULL];
}

// 9宫格图片布局
- (void)nineGrid
{
    for (UIImageView *imgv in _editv.subviews)
    {
        if ([imgv isKindOfClass:[UIImageView class]]) {
            [imgv removeFromSuperview];
        }
    }
    
    CGFloat width = 70;
    CGFloat widthSpace = (ScreenWidth - 15*4 - 70*4) / 3.0;
    CGFloat heightSpace = 10;
    
    NSInteger count = _imageArray.count;
    _imageArray.count > 9 ? (count = 9) : (count = _imageArray.count);
    
    for (int i=0; i<count; i++)
    {
        UIImageView *imgv = [[UIImageView alloc] initWithFrame:CGRectMake(15+(width+widthSpace)*(i%4), (i/4)*(width+heightSpace) + CGRectGetMaxY(_textView.frame)+15, width, width)];
        imgv.image = _imageArray[i];
        imgv.userInteractionEnabled = YES;
        [_editv addSubview:imgv];
        
        UIButton *delete = [UIButton buttonWithType:UIButtonTypeCustom];
        delete.frame = CGRectMake(width-16, 0, 16, 16);
        delete.backgroundColor = [UIColor greenColor];
        [delete addTarget:self action:@selector(deleteEvent:) forControlEvents:UIControlEventTouchUpInside];
        delete.tag = 10+i;
        [imgv addSubview:delete];
        
        if (i == _imageArray.count - 1)
        {
            if (_imageArray.count % 4 == 0) {
                _addPic.frame = CGRectMake(15, CGRectGetMaxY(imgv.frame) + heightSpace, 70, 70);
            } else {
                _addPic.frame = CGRectMake(CGRectGetMaxX(imgv.frame) + widthSpace, CGRectGetMinY(imgv.frame), 70, 70);
            }
            
            _editv.frame = CGRectMake(15, 50, ScreenWidth-15*2, CGRectGetMaxY(_addPic.frame)+20);
        }
    }
}

// 删除照片
- (void)deleteEvent:(UIButton *)sender
{
    UIButton *btn = (UIButton *)sender;
    [_imageArray removeObjectAtIndex:btn.tag-10];
    
    [self nineGrid];
    
    if (_imageArray.count == 0)
    {
        _addPic.frame = CGRectMake(15, CGRectGetMaxY(_textView.frame)+15, 70, 70);
        _editv.frame = CGRectMake(15, 50, ScreenWidth-15*2, CGRectGetMaxY(_addPic.frame)+20);
    }
}

#pragma mark - ZYQAssetPickerController Delegate

- (void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
                   {
                       for (int i=0; i<assets.count; i++)
                       {
                           ALAsset *asset = assets[i];
                           UIImage *tempImg = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
                           [_imageArray addObject:tempImg];
                           
                           dispatch_async(dispatch_get_main_queue(), ^{
                               [self nineGrid];
                           });
                       }
                   });
}

#pragma makr - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _placeholderLabel.text = @"在此编辑您的而分享内容!";
    } else {
        _placeholderLabel.text = @"";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end


AddPicture.zip (139.86 KB, 下载次数: 1670)
1535 1 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542


代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表