-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathMRUtil.m
More file actions
95 lines (85 loc) · 2.72 KB
/
MRUtil.m
File metadata and controls
95 lines (85 loc) · 2.72 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// MRUtil.m
// FFmpegTutorial-macOS
//
// Created by qianlongxu on 2020/12/2.
//
#import "MRUtil.h"
#import <CoreGraphics/CoreGraphics.h>
#import <AppKit/NSImage.h>
@implementation MRUtil
+ (NSArray <NSString *>*)videoType
{
return @[
@"wmv",
@"avi",
@"rm",
@"rmvb",
@"mpg",
@"m2v",
@"mpeg",
@"3gp",
@"mov",
@"mp4",
@"mkv",
@"flv",
@"ts",
@"webm"
];
}
+ (NSDictionary *)makeBookmarkWithURL:(NSURL *)url
{
NSData *bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
| NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess includingResourceValuesForKeys:nil relativeToURL:nil error:nil];
if (bookmark) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:url forKey:@"url"];
[dic setObject:bookmark forKey:@"bookmark"];
return [dic copy];
}
return nil;
}
+ (NSArray <NSDictionary *>*)scanFolderWithPath:(NSString *)dir filter:(NSArray<NSString *>*)types
{
NSError *error = nil;
NSMutableArray *bookmarkArr = [NSMutableArray arrayWithCapacity:3];
NSArray<NSString *> *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:&error];
if (!error && contents) {
for (NSString *c in contents) {
NSString*item = [dir stringByAppendingPathComponent:c];
NSURL*item_url = [NSURL fileURLWithPath:item];
NSString *pathExtension = [[item_url pathExtension] lowercaseString];
BOOL add = NO;
if ([types count] > 0) {
if ([types containsObject:pathExtension]) {
add = YES;
}
} else {
add = YES;
}
if (add) {
NSDictionary *dic = [[self class] makeBookmarkWithURL:item_url];
[bookmarkArr addObject:dic];
}
}
//按照文件名排序
[bookmarkArr sortUsingComparator:^NSComparisonResult(NSDictionary * obj1, NSDictionary * obj2) {
NSURL *url1 = obj1[@"url"];
NSURL *url2 = obj2[@"url"];
return (NSComparisonResult)[[url1 lastPathComponent] compare:[url2 lastPathComponent] options:NSNumericSearch];
}];
return [bookmarkArr copy];
}
return nil;
}
+ (CGImageRef)nsImage2cg:(NSImage *)src
{
NSData * imageData = [src TIFFRepresentation];
CGImageRef imageRef = NULL;
if (imageData) {
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
}
return imageRef;
}
@end