最近要搞一个破事,就是项目要做一个表情包(其实是APP推广,跟表情毛关系没有)既然是推广,那我肯定不能让苹果看到,于是要做一个本地的合法表情包跟一个能够联网获取图片的表情包,在过审之后替换掉= = 为了以防万一坑了已经上线的APP,我的打算是单独做一个iMessage的APP



MSStickerBrowserViewController *browserVc = [[MSStickerBrowserViewController alloc]initWithStickerSize:MSStickerSizeSmall];
[self addChildViewController:browserVc];
[self.view addSubview:browserVc.view];
browserVc.stickerBrowserView.backgroundColor = [UIColor whiteColor];
//设置数据源,如果只是单纯的联网,这个就不要写了,我是为了要过审
if (_isKH==YES) {
browserVc.stickerBrowserView.dataSource = self;
}
browserVc.view.translatesAutoresizingMaskIntoConstraints = NO;
//自动布局
[self.view.topAnchor constraintEqualToAnchor:browserVc.view.topAnchor].active = YES;
[self.view.bottomAnchor constraintEqualToAnchor:browserVc.view.bottomAnchor].active = YES;
[self.view.leftAnchor constraintEqualToAnchor:browserVc.view.leftAnchor].active = YES;
[self.view.rightAnchor constraintEqualToAnchor:browserVc.view.rightAnchor].active = YES;
创建好了之后我们在上面放一个UICollectionView,点击cell 发图片,这样就大功告成了,那么创建个UICollectionView吧
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
layout.minimumInteritemSpacing = 10;
layout.minimumLineSpacing = 10;
layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width-60)/3.0, ([UIScreen mainScreen].bounds.size.width-60)/3.0);
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
_collection = [[UICollectionView alloc]initWithFrame:browserVc.view.bounds collectionViewLayout:layout];
_collection.delegate = self;
_collection.backgroundColor = [UIColor whiteColor];
_collection.dataSource = self;
[browserVc.view addSubview:_collection];
[_collection registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cell"];
#pragma mark collectionView 代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.stickersArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
[cell.imgView sd_setImageWithURL:[NSURL URLWithString:self.stickersArray[indexPath.row]]];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[[SDWebImageManager sharedManager]loadImageWithURL:[NSURL URLWithString:self.stickersArray[indexPath.row]] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
MSMessage * message = [[MSMessage alloc]init];
// message.URL = [NSURL URLWithString:@"http://www.baidu.com"];
// message.accessibilityLabel = @"accessibility";
// message.summaryText = @"summaryText";
MSMessageTemplateLayout * layout = [[MSMessageTemplateLayout alloc]init];
layout.caption = @"caption";
layout.subcaption = @"subcaption";
// layout.trailingCaption = @"trailing";
// layout.trailingSubcaption =@"subtrailing";
layout.image = image;
// layout.imageTitle = @"标题";
// layout.imageSubtitle = @"子标题";
message.layout = layout;
[self.activeConversation insertMessage:message completionHandler:nil];
}];
}
以上就是主要代码了,写这个的原因主要是网上都是一堆复制黏贴的本地图包,本来想直接贴代码,但是那样给人看还不如不看,就这样搞一波吧