封面尺寸获取

920 创建: 更新: 2020/4/26 11:20 本文总阅读量

补充

4-26补充

20230826补充

CGFloat fixelW = CGImageGetWidth(image.CGImage);
CGFloat fixelH = CGImageGetHeight(image.CGImage);

方法一

NSURL *videoURl = [NSURL URLWithString:_playUrl];
AVAsset *asset = [AVAsset assetWithURL:videoURl];
[asset loadValuesAsynchronouslyForKeys:@[@"tracks"] completionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (asset.playable) {
            NSArray *array = asset.tracks;
            CGSize videoSize = CGSizeZero;
            for (AVAssetTrack *track in array) {
                if ([track.mediaType isEqualToString:AVMediaTypeVideo]) {
                    videoSize = track.naturalSize;
                }
            }
            NSLog(@"width----------------------->%f",videoSize.width);
            NSLog(@"height----------------------->%f",videoSize.height);
            if(videoSize.width>videoSize.height){
                playerModel.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
            }else{
                playerModel.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
            }
        }
    });
}];

方法二

NSMutableString *imageURL = [NSMutableString stringWithFormat:@"%@",_img];
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL URLWithString:imageURL], NULL);
NSDictionary* imageHeader = (__bridge NSDictionary*) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

_h = [NSString stringWithFormat:@"%@",[imageHeader objectForKey:@"PixelHeight"]];
_w = [NSString stringWithFormat:@"%@",[imageHeader objectForKey:@"PixelWidth"]];