CustomStudyEntity.m 2.61 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
//
//  CustomStudyEntity.m
//  Lighting
//
//  Created by 曹云霄 on 2016/12/5.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "CustomStudyEntity.h"
#import <AVFoundation/AVFoundation.h>
@implementation CustomStudyEntity


- (CGFloat)contentHeight
{
    if (!_contentHeight) {
        _contentHeight = [self calculateStudyIntroductionHeight:self.content]+74;
    }
    return _contentHeight;
}

- (CGFloat)teacherIntroHeight
{
    if (!_teacherIntroHeight) {
        _teacherIntroHeight = [self calculateStudyIntroductionHeight:self.teacherIntro]+106;
    }
    return _teacherIntroHeight;
}

- (CGFloat)personHeight
{
    if (!_personHeight) {
        _personHeight = [self calculateStudyIntroductionHeight:self.suitabler]+74;
    }
    return _personHeight;
}

- (NSString *)videoLength
{
    if (!_videoLength) {
        _videoLength = [self calculateVideoLength];
    }
    return _videoLength;
}

#pragma mark - 计算视频长度
- (NSString *)calculateVideoLength
{
    if ([self.attachment.fileUrl rangeOfString:@".mp4"].location != NSNotFound) {
        NSURL *movieURL = [NSURL URLWithString:self.attachment.fileUrl];
        NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
                                                         forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
        AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:movieURL options:opts];  // 初始化视频媒体文件
        NSInteger interval = urlAsset.duration.value / urlAsset.duration.timescale; // 获取视频总时长,单位秒
        NSUInteger secondPerDay = 24 * 60 * 60;
        NSUInteger secondPerHour = 60 * 60;
        NSUInteger secondPerMinute = 60;
        
        // 剩余小时不应该大于24小时,所以应该先除去满足一天的秒数,再计算还剩下多少小时
        NSInteger hour = interval % secondPerDay / secondPerHour;
        // 剩余分钟数与上面同理
        NSInteger minute = interval % secondPerHour / secondPerMinute;
        // 剩余秒数直接等于秒数对每分钟秒数所取的余数
        NSInteger second = interval % secondPerMinute;
        NSMutableString *string = [NSMutableString string];
        if (hour) {
            [string appendString:[NSString stringWithFormat:@"%02zd:",hour]];
        }
        [string appendString:[NSString stringWithFormat:@"%02zd:%02zd",minute,second]];
        return string;
        }
    return nil;
}


#pragma mark - 计算高度
- (CGFloat)calculateStudyIntroductionHeight:(NSString *)content
{
79
    return [content heightWithWidth:ScreenWidth*2/3-28*2 andFont:12].height;
曹云霄's avatar
曹云霄 committed
80 81 82 83 84
}



@end