Initial commit
This commit is contained in:
+346
@@ -0,0 +1,346 @@
|
||||
//
|
||||
// NTESSessionUtil.m
|
||||
// NIMDemo
|
||||
//
|
||||
// Created by ght on 15-1-27.
|
||||
// Copyright (c) 2015年 Netease. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NTESSessionUtil.h"
|
||||
#import "NTESLoginManager.h"
|
||||
#import "NTESSnapchatAttachment.h"
|
||||
#import "NTESJanKenPonAttachment.h"
|
||||
#import "NTESChartletAttachment.h"
|
||||
#import "UIImage+NTES.h"
|
||||
#import "NTESDataManager.h"
|
||||
#import "NTESSnapchatAttachment.h"
|
||||
#import "NTESWhiteboardAttachment.h"
|
||||
double OnedayTimeIntervalValue = 24*60*60; //一天的秒数
|
||||
@implementation NTESSessionUtil
|
||||
|
||||
+ (CGSize)getImageSizeWithImageOriginSize:(CGSize)originSize
|
||||
minSize:(CGSize)imageMinSize
|
||||
maxSize:(CGSize)imageMaxSiz
|
||||
{
|
||||
CGSize size;
|
||||
NSInteger imageWidth = originSize.width ,imageHeight = originSize.height;
|
||||
NSInteger imageMinWidth = imageMinSize.width, imageMinHeight = imageMinSize.height;
|
||||
NSInteger imageMaxWidth = imageMaxSiz.width, imageMaxHeight = imageMaxSiz.height;
|
||||
if (imageWidth > imageHeight) //宽图
|
||||
{
|
||||
size.height = imageMinHeight; //高度取最小高度
|
||||
size.width = imageWidth * imageMinHeight / imageHeight;
|
||||
if (size.width > imageMaxWidth)
|
||||
{
|
||||
size.width = imageMaxWidth;
|
||||
}
|
||||
}
|
||||
else if(imageWidth < imageHeight)//高图
|
||||
{
|
||||
size.width = imageMinWidth;
|
||||
size.height = imageHeight *imageMinWidth / imageWidth;
|
||||
if (size.height > imageMaxHeight)
|
||||
{
|
||||
size.height = imageMaxHeight;
|
||||
}
|
||||
}
|
||||
else//方图
|
||||
{
|
||||
if (imageWidth > imageMaxWidth)
|
||||
{
|
||||
size.width = imageMaxWidth;
|
||||
size.height = imageMaxHeight;
|
||||
}
|
||||
else if(imageWidth > imageMinWidth)
|
||||
{
|
||||
size.width = imageWidth;
|
||||
size.height = imageHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
size.width = imageMinWidth;
|
||||
size.height = imageMinHeight;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
+(BOOL)isTheSameDay:(NSTimeInterval)currentTime compareTime:(NSDateComponents*)older
|
||||
{
|
||||
NSCalendarUnit currentComponents = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSHourCalendarUnit | NSMinuteCalendarUnit);
|
||||
NSDateComponents *current = [[NSCalendar currentCalendar] components:currentComponents fromDate:[NSDate dateWithTimeIntervalSinceNow:currentTime]];
|
||||
|
||||
return current.year == older.year && current.month == older.month && current.day == older.day;
|
||||
}
|
||||
|
||||
+(NSString*)weekdayStr:(NSInteger)dayOfWeek
|
||||
{
|
||||
static NSDictionary *daysOfWeekDict = nil;
|
||||
daysOfWeekDict = @{@(1):@"星期日",
|
||||
@(2):@"星期一",
|
||||
@(3):@"星期二",
|
||||
@(4):@"星期三",
|
||||
@(5):@"星期四",
|
||||
@(6):@"星期五",
|
||||
@(7):@"星期六",};
|
||||
return [daysOfWeekDict objectForKey:@(dayOfWeek)];
|
||||
}
|
||||
|
||||
|
||||
+(NSDateComponents*)stringFromTimeInterval:(NSTimeInterval)messageTime components:(NSCalendarUnit)components
|
||||
{
|
||||
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:components fromDate:[NSDate dateWithTimeIntervalSince1970:messageTime]];
|
||||
return dateComponents;
|
||||
}
|
||||
|
||||
|
||||
+ (NSString *)showNick:(NSString*)uid inSession:(NIMSession*)session{
|
||||
|
||||
NSString *nickname = nil;
|
||||
if (session.sessionType == NIMSessionTypeTeam)
|
||||
{
|
||||
NIMTeamMember *member = [[NIMSDK sharedSDK].teamManager teamMember:uid inTeam:session.sessionId];
|
||||
nickname = member.nickname;
|
||||
}
|
||||
if (!nickname.length) {
|
||||
NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:uid];
|
||||
nickname = info.showName;
|
||||
}
|
||||
return nickname;
|
||||
}
|
||||
|
||||
|
||||
+(NSString*)showTime:(NSTimeInterval) msglastTime showDetail:(BOOL)showDetail
|
||||
{
|
||||
//今天的时间
|
||||
NSDate * nowDate = [NSDate date];
|
||||
NSDate * msgDate = [NSDate dateWithTimeIntervalSince1970:msglastTime];
|
||||
NSString *result = nil;
|
||||
NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSHourCalendarUnit | NSMinuteCalendarUnit);
|
||||
NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
|
||||
NSDateComponents *msgDateComponents = [[NSCalendar currentCalendar] components:components fromDate:msgDate];
|
||||
|
||||
NSInteger hour = msgDateComponents.hour;
|
||||
result = [NTESSessionUtil getPeriodOfTime:hour withMinute:msgDateComponents.minute];
|
||||
if (hour > 12)
|
||||
{
|
||||
hour = hour - 12;
|
||||
}
|
||||
if(nowDateComponents.day == msgDateComponents.day) //同一天,显示时间
|
||||
{
|
||||
result = [[NSString alloc] initWithFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute];
|
||||
}
|
||||
else if(nowDateComponents.day == (msgDateComponents.day+1))//昨天
|
||||
{
|
||||
result = showDetail? [[NSString alloc] initWithFormat:@"昨天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"昨天";
|
||||
}
|
||||
else if(nowDateComponents.day == (msgDateComponents.day+2)) //前天
|
||||
{
|
||||
result = showDetail? [[NSString alloc] initWithFormat:@"前天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"前天";
|
||||
}
|
||||
else if([nowDate timeIntervalSinceDate:msgDate] < 7 * OnedayTimeIntervalValue)//一周内
|
||||
{
|
||||
NSString *weekDay = [NTESSessionUtil weekdayStr:msgDateComponents.weekday];
|
||||
result = showDetail? [weekDay stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : weekDay;
|
||||
}
|
||||
else//显示日期
|
||||
{
|
||||
NSString *day = [NSString stringWithFormat:@"%zd-%zd-%zd", msgDateComponents.year, msgDateComponents.month, msgDateComponents.day];
|
||||
result = showDetail? [day stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute]:day;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
+ (NSString *)getPeriodOfTime:(NSInteger)time withMinute:(NSInteger)minute
|
||||
{
|
||||
NSInteger totalMin = time *60 + minute;
|
||||
NSString *showPeriodOfTime = @"";
|
||||
if (totalMin > 0 && totalMin <= 5 * 60)
|
||||
{
|
||||
showPeriodOfTime = @"凌晨";
|
||||
}
|
||||
else if (totalMin > 5 * 60 && totalMin < 12 * 60)
|
||||
{
|
||||
showPeriodOfTime = @"上午";
|
||||
}
|
||||
else if (totalMin >= 12 * 60 && totalMin <= 18 * 60)
|
||||
{
|
||||
showPeriodOfTime = @"下午";
|
||||
}
|
||||
else if ((totalMin > 18 * 60 && totalMin <= (23 * 60 + 59)) || totalMin == 0)
|
||||
{
|
||||
showPeriodOfTime = @"晚上";
|
||||
}
|
||||
return showPeriodOfTime;
|
||||
}
|
||||
|
||||
|
||||
+ (void)sessionWithInputURL:(NSURL*)inputURL
|
||||
outputURL:(NSURL*)outputURL
|
||||
blockHandler:(void (^)(AVAssetExportSession*))handler
|
||||
{
|
||||
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
|
||||
AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset
|
||||
presetName:AVAssetExportPresetMediumQuality];
|
||||
session.outputURL = outputURL;
|
||||
session.outputFileType = AVFileTypeMPEG4; // 支持安卓某些机器的视频播放
|
||||
session.shouldOptimizeForNetworkUse = YES;
|
||||
[session exportAsynchronouslyWithCompletionHandler:^(void)
|
||||
{
|
||||
handler(session);
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
+ (NSDictionary *)dictByJsonData:(NSData *)data
|
||||
{
|
||||
NSDictionary *dict = nil;
|
||||
if ([data isKindOfClass:[NSData class]])
|
||||
{
|
||||
NSError *error = nil;
|
||||
dict = [NSJSONSerialization JSONObjectWithData:data
|
||||
options:0
|
||||
error:&error];
|
||||
if (error) {
|
||||
NSLog(@"dictByJsonData failed %@ error %@",data,error);
|
||||
}
|
||||
}
|
||||
return [dict isKindOfClass:[NSDictionary class]] ? dict : nil;
|
||||
}
|
||||
|
||||
|
||||
+ (NSDictionary *)dictByJsonString:(NSString *)jsonString
|
||||
{
|
||||
if (!jsonString.length) {
|
||||
return nil;
|
||||
}
|
||||
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
|
||||
return [NTESSessionUtil dictByJsonData:data];
|
||||
}
|
||||
|
||||
|
||||
+ (AVMutableVideoComposition *) getVideoComposition:(AVAsset *)asset
|
||||
{
|
||||
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
|
||||
AVMutableComposition *composition = [AVMutableComposition composition];
|
||||
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
|
||||
CGSize videoSize = videoTrack.naturalSize;
|
||||
BOOL isPortrait_ = [NTESSessionUtil isVideoPortrait:asset];
|
||||
if(isPortrait_) {
|
||||
videoSize = CGSizeMake(videoSize.height, videoSize.width);
|
||||
}
|
||||
composition.naturalSize = videoSize;
|
||||
videoComposition.renderSize = videoSize;
|
||||
|
||||
videoComposition.frameDuration = CMTimeMakeWithSeconds( 1 / videoTrack.nominalFrameRate, 600);
|
||||
AVMutableCompositionTrack *compositionVideoTrack;
|
||||
compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
|
||||
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:videoTrack atTime:kCMTimeZero error:nil];
|
||||
AVMutableVideoCompositionLayerInstruction *layerInst;
|
||||
layerInst = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
|
||||
[layerInst setTransform:videoTrack.preferredTransform atTime:kCMTimeZero];
|
||||
AVMutableVideoCompositionInstruction *inst = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
|
||||
inst.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
|
||||
inst.layerInstructions = [NSArray arrayWithObject:layerInst];
|
||||
videoComposition.instructions = [NSArray arrayWithObject:inst];
|
||||
return videoComposition;
|
||||
}
|
||||
|
||||
|
||||
+ (BOOL) isVideoPortrait:(AVAsset *)asset
|
||||
{
|
||||
BOOL isPortrait = NO;
|
||||
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
|
||||
if([tracks count] > 0) {
|
||||
AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
|
||||
|
||||
CGAffineTransform t = videoTrack.preferredTransform;
|
||||
// Portrait
|
||||
if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0)
|
||||
{
|
||||
isPortrait = YES;
|
||||
}
|
||||
// PortraitUpsideDown
|
||||
if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) {
|
||||
|
||||
isPortrait = YES;
|
||||
}
|
||||
// LandscapeRight
|
||||
if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0)
|
||||
{
|
||||
isPortrait = NO;
|
||||
}
|
||||
// LandscapeLeft
|
||||
if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0)
|
||||
{
|
||||
isPortrait = NO;
|
||||
}
|
||||
}
|
||||
return isPortrait;
|
||||
}
|
||||
|
||||
|
||||
+ (NSString *)tipOnMessageRevoked:(NIMMessage *)message
|
||||
{
|
||||
BOOL isFromMe = [message.from isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]];
|
||||
NSString *tip = @"你";
|
||||
if (!isFromMe) {
|
||||
switch (message.session.sessionType) {
|
||||
case NIMSessionTypeP2P:
|
||||
tip = @"对方";
|
||||
break;
|
||||
case NIMSessionTypeTeam:{
|
||||
NIMKitInfo *info = [[NTESDataManager sharedInstance] infoByUser:message.from inSession:message.session];
|
||||
tip = info.showName;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return [NSString stringWithFormat:@"%@撤回了一条消息",tip];
|
||||
}
|
||||
|
||||
|
||||
+ (BOOL)canMessageBeForwarded:(NIMMessage *)message
|
||||
{
|
||||
if (!message.isReceivedMsg && message.deliveryState == NIMMessageDeliveryStateFailed) {
|
||||
return NO;
|
||||
}
|
||||
id<NIMMessageObject> messageobject = message.messageObject;
|
||||
if ([messageobject isKindOfClass:[NIMCustomObject class]]
|
||||
&& ([[(NIMCustomObject *)messageobject attachment] isKindOfClass:[NTESSnapchatAttachment class]]
|
||||
|| [[(NIMCustomObject *)messageobject attachment] isKindOfClass:[NTESWhiteboardAttachment class]])) {
|
||||
return NO;
|
||||
}
|
||||
if ([messageobject isKindOfClass:[NIMNotificationObject class]]) {
|
||||
return NO;
|
||||
}
|
||||
if ([messageobject isKindOfClass:[NIMTipObject class]]) {
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (BOOL)canMessageBeRevoked:(NIMMessage *)message
|
||||
{
|
||||
BOOL isFromMe = [message.from isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]];
|
||||
BOOL isToMe = [message.session.sessionId isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]];
|
||||
BOOL isDeliverFailed = !message.isReceivedMsg && message.deliveryState == NIMMessageDeliveryStateFailed;
|
||||
if (!isFromMe || isToMe || isDeliverFailed) {
|
||||
return NO;
|
||||
}
|
||||
id<NIMMessageObject> messageobject = message.messageObject;
|
||||
if ([messageobject isKindOfClass:[NIMTipObject class]]
|
||||
|| [messageobject isKindOfClass:[NIMNotificationObject class]]) {
|
||||
return NO;
|
||||
}
|
||||
if ([messageobject isKindOfClass:[NIMCustomObject class]]
|
||||
&& ([[(NIMCustomObject *)messageobject attachment] isKindOfClass:[NTESWhiteboardAttachment class]])) {
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user