iMessage联网获取图片表情

329 阅读2分钟

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

image.png
绿框的那个是只能做本地表情包的,就是拖图片进去然后ok那种,没卵子用,我们点击红框 进来看到这个viewController,这就是我们搞事情的地方了
image.png
之前我的思路是下载好图片然后存入Documents然后再取出来作为本地表情包,但是这样有个问题,MSSticker,这个就是表情包的对象,如何吧UIImage转为MSSticker,这个我弄了好久,你们看下图
image.png
只有三个内容,而且两个属性是只读,另一个只能从bundle读取,所以这条路是走不通的 那么既然这是一个viewController,那么我们就在viewController上搞事情吧。 首先网上找到一段代码,创建一个viewController

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];
    }];
}

以上就是主要代码了,写这个的原因主要是网上都是一堆复制黏贴的本地图包,本来想直接贴代码,但是那样给人看还不如不看,就这样搞一波吧