FileInfo.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //
  2. // FileInfo.m
  3. // iPhoneFTP
  4. //
  5. // Created by Zhou Weikuan on 10-6-15.
  6. // Copyright 2010 sino. All rights reserved.
  7. //
  8. #import "FileInfo.h"
  9. @implementation FileInfo
  10. + (NSURL *)smartURLForString:(NSString *)str
  11. {
  12. NSURL * result;
  13. NSString * trimmedStr;
  14. NSRange schemeMarkerRange;
  15. NSString * scheme;
  16. assert(str != nil);
  17. result = nil;
  18. trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  19. if ( (trimmedStr != nil) && (trimmedStr.length != 0) ) {
  20. schemeMarkerRange = [trimmedStr rangeOfString:@"://"];
  21. if (schemeMarkerRange.location == NSNotFound) {
  22. result = [NSURL URLWithString:[NSString stringWithFormat:@"ftp://%@", trimmedStr]];
  23. } else {
  24. scheme = [trimmedStr substringWithRange:NSMakeRange(0, schemeMarkerRange.location)];
  25. assert(scheme != nil);
  26. if ( ([scheme compare:@"ftp" options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
  27. result = [NSURL URLWithString:trimmedStr];
  28. } else {
  29. // It looks like this is some unsupported URL scheme.
  30. }
  31. }
  32. }
  33. return result;
  34. }
  35. + (uint64_t) getFTPStreamSize:(CFReadStreamRef)stream {
  36. return 0ll;
  37. }
  38. + (NSString*) pathForDocument {
  39. NSString *s = [NSString stringWithFormat:@"%@/Library/Caches/",NSHomeDirectory()];
  40. //NSLog(@"%@",s);
  41. return s;
  42. }
  43. + (uint64_t) getFileSize:(NSString *)filePath {
  44. NSFileManager * fileManager = [NSFileManager defaultManager];
  45. if (![filePath isKindOfClass:[NSString class]]) {
  46. return 0;
  47. }
  48. NSDictionary * dict = [fileManager attributesOfItemAtPath:filePath error:nil];
  49. return [dict fileSize];
  50. }
  51. +(void)createDir:(NSString*)s{
  52. NSFileManager *fileManager = [NSFileManager defaultManager];
  53. if (![fileManager fileExistsAtPath:s]) {
  54. [fileManager createDirectoryAtPath:s withIntermediateDirectories:YES attributes:nil error:nil];
  55. }
  56. fileManager = nil;
  57. }
  58. +(NSString*)getUUIDFileName:(NSString*)ext{
  59. NSString* s =[NSUUID UUID].UUIDString;
  60. s = [[s stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
  61. NSString *fileName = [NSString stringWithFormat:@"%@.%@",s,ext];
  62. return [myTempFilePath stringByAppendingPathComponent:fileName];
  63. }
  64. + (NSString *)getAttachImgFilePath:(NSString *)ext{
  65. NSString *s = [NSUUID UUID].UUIDString;
  66. s = [[s stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
  67. NSString *filePath = [NSString stringWithFormat:@"%@%@.%@",myTempFilePath,s,ext];
  68. return filePath;
  69. }
  70. + (NSString *)getComImgFileName:(NSString *)ext{
  71. NSString *s = [NSUUID UUID].UUIDString;
  72. s = [[s stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
  73. NSString *fileName = [NSString stringWithFormat:@"%@%@/comImgs/%@.%@",docFilePath,g_myself.userId,s,ext];
  74. return fileName;
  75. }
  76. + (void)deleteComImg{
  77. NSString *imgPath = [NSString stringWithFormat:@"%@%@/comImgs/comImg.png",docFilePath,g_myself.userId];
  78. if ([[NSFileManager defaultManager] fileExistsAtPath:imgPath]) {
  79. [[NSFileManager defaultManager] removeItemAtPath:imgPath error:nil];
  80. }
  81. }
  82. +(void)deleleFileAndDir:(NSString*)s{
  83. NSString* dir=s;
  84. NSString* Path;
  85. NSArray *contentOfFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:NULL];
  86. for (NSString *aPath in contentOfFolder) {
  87. Path = [dir stringByAppendingPathComponent:aPath];
  88. BOOL isDir;
  89. if ([[NSFileManager defaultManager] fileExistsAtPath:Path isDirectory:&isDir])
  90. {
  91. if(isDir)
  92. [FileInfo deleleFileAndDir:Path];
  93. else{
  94. [[NSFileManager defaultManager] removeItemAtPath:Path error:nil];
  95. [[NSRunLoop currentRunLoop]runUntilDate:[NSDate distantPast]];//重要
  96. }
  97. }
  98. }
  99. contentOfFolder = nil;
  100. }
  101. +(NSMutableArray*)getFiles:(NSString*)s{
  102. NSMutableArray* a = [[NSMutableArray alloc] init];
  103. NSString* dir=s;
  104. NSString* Path;
  105. NSArray *contentOfFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:NULL];
  106. for (NSString *aPath in contentOfFolder) {
  107. Path = [dir stringByAppendingPathComponent:aPath];
  108. BOOL isDir;
  109. if ([[NSFileManager defaultManager] fileExistsAtPath:Path isDirectory:&isDir])
  110. {
  111. if(!isDir){
  112. [a addObject:Path];
  113. }
  114. }
  115. }
  116. contentOfFolder = nil;
  117. return a;
  118. }
  119. +(NSMutableArray *)getFilesName:(NSString *)s {
  120. NSMutableArray* a = [[NSMutableArray alloc] init];
  121. NSString* dir=s;
  122. NSString* Path;
  123. NSArray *contentOfFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:NULL];
  124. for (NSString *aPath in contentOfFolder) {
  125. Path = [dir stringByAppendingPathComponent:aPath];
  126. BOOL isDir;
  127. if ([[NSFileManager defaultManager] fileExistsAtPath:Path isDirectory:&isDir])
  128. {
  129. if(!isDir){
  130. [a addObject:[Path lastPathComponent]];
  131. }
  132. }
  133. }
  134. contentOfFolder = nil;
  135. return a;
  136. }
  137. //获取视频的总时长
  138. + (double )getVideoTimeFromVideo:(NSString *)video{
  139. NSURL* url;
  140. if( [video rangeOfString:@"http://"].location == NSNotFound && [video rangeOfString:@"https://"].location == NSNotFound)
  141. url = [NSURL fileURLWithPath:video];
  142. else
  143. url = [NSURL URLWithString:video];
  144. AVURLAsset *avurl = [AVURLAsset assetWithURL:url];
  145. CMTime time = [avurl duration];
  146. int alltime = ceil(time.value/time.timescale);
  147. return alltime;
  148. }
  149. //获取指定时间的帧数
  150. +(UIImage *)getImagesFromVideo:(NSString *)video withTimeInterval:(double )time{
  151. NSURL* url;
  152. if( [video rangeOfString:@"http://"].location == NSNotFound && [video rangeOfString:@"https://"].location == NSNotFound)
  153. url = [NSURL fileURLWithPath:video];
  154. else
  155. url = [NSURL URLWithString:video];
  156. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
  157. NSParameterAssert(asset);
  158. AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  159. generator.appliesPreferredTrackTransform = YES;
  160. generator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
  161. generator.requestedTimeToleranceAfter = kCMTimeZero;
  162. generator.requestedTimeToleranceBefore = kCMTimeZero;
  163. NSError *error = nil;
  164. CGImageRef cgImage = NULL;
  165. cgImage = [generator copyCGImageAtTime:CMTimeMakeWithSeconds(time, 600) actualTime:nil error:&error];
  166. if (!cgImage) {
  167. NSLog(@"获取视频第一帧图片失败:%@",error);
  168. return nil;
  169. }
  170. UIImage *image = [UIImage imageWithCGImage:cgImage];
  171. CFRelease(cgImage);
  172. return image;
  173. }
  174. +(UIImage*)getFirstImageFromVideo:(NSString*)video{
  175. NSString *filePath = [NSString stringWithFormat:@"%@%@.jpg",myTempFilePath,[[video lastPathComponent] stringByDeletingPathExtension]];
  176. if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
  177. return [UIImage imageWithContentsOfFile:filePath];
  178. NSURL* url;
  179. if( [video rangeOfString:@"http://"].location == NSNotFound && [video rangeOfString:@"https://"].location == NSNotFound)
  180. url = [NSURL fileURLWithPath:video];
  181. else
  182. url = [NSURL URLWithString:video];
  183. //获取视频的首帧作为缩略图
  184. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
  185. NSParameterAssert(asset);
  186. AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  187. generator.appliesPreferredTrackTransform = YES;
  188. generator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
  189. NSError *error = nil;
  190. CGImageRef cgImage = [generator copyCGImageAtTime:CMTimeMakeWithSeconds(0.8, 600) actualTime:nil error:&error];
  191. if (!cgImage) {
  192. NSLog(@"获取视频第一帧图片失败:%@",error);
  193. return nil;
  194. }
  195. //保存图片到本地
  196. NSData * imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:cgImage], 1);
  197. NSError *imageerror =nil;
  198. BOOL isSuccess = [imageData writeToFile:filePath atomically:YES];
  199. if (!isSuccess) {
  200. NSLog(@"获取视频第一帧图片写入失败,%@",imageerror);
  201. }
  202. CFRelease(cgImage);
  203. return [UIImage imageWithCGImage:cgImage];
  204. }
  205. +(void)getFirstImageFromVideo:(NSString*)video imageView:(UIImageView*)iv{
  206. if (!video) {
  207. return;
  208. }
  209. dispatch_async(dispatch_get_global_queue(0,0), ^{
  210. UIImage * image=nil;
  211. image = [FileInfo getFirstImageFromVideo:video];
  212. dispatch_async(dispatch_get_main_queue(), ^{
  213. CGRect rect;//然后,将此部分从图片中剪切出来
  214. if (image.size.width > image.size.height) {
  215. rect = CGRectMake((image.size.width - image.size.height) / 2, 0, image.size.height, image.size.height);
  216. }else {
  217. rect = CGRectMake(0, (image.size.height - image.size.width) / 2, image.size.width, image.size.width);
  218. }
  219. CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
  220. UIImage *image1 = [UIImage imageWithCGImage:imageRef];
  221. if (imageRef) {
  222. CFRelease(imageRef);
  223. }
  224. iv.image = image1;
  225. });
  226. });
  227. }
  228. // 根据imageview大小剪裁图片
  229. +(void)getFirstImageFromVideoWithImageVIew:(NSString *)video imageView:(UIImageView*)iv {
  230. if (!video) {
  231. return;
  232. }
  233. dispatch_semaphore_t sem = dispatch_semaphore_create(0);
  234. dispatch_async(dispatch_get_global_queue(0,0), ^{
  235. UIImage * image=nil;
  236. image = [FileInfo getFirstImageFromVideo:video];
  237. dispatch_semaphore_signal(sem);
  238. dispatch_async(dispatch_get_main_queue(), ^{
  239. dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
  240. CGFloat scale = iv.frame.size.width / iv.frame.size.height;
  241. CGRect rect;//然后,将此部分从图片中剪切出来
  242. if ((image.size.width / image.size.height) > scale) {
  243. rect = CGRectMake((image.size.width - (image.size.height * scale)) / 2, 0, image.size.height * scale, image.size.height);
  244. }else {
  245. rect = CGRectMake(0, (image.size.height - (image.size.width / scale)) / 2, image.size.width, (image.size.width / scale));
  246. }
  247. CGImageRef imageRef = [image CGImage];
  248. imageRef = CGImageCreateWithImageInRect(imageRef, rect);
  249. UIImage *image1 = [UIImage imageWithCGImage:imageRef];
  250. iv.image = image1;
  251. if (imageRef) {
  252. CFRelease(imageRef);
  253. }
  254. });
  255. });
  256. }
  257. +(void)getFullFirstImageFromVideo:(NSString*)video imageView:(UIImageView*)iv{
  258. if (!video) {
  259. return;
  260. }
  261. dispatch_async(dispatch_get_global_queue(0,0), ^{
  262. UIImage * image=nil;
  263. image = [FileInfo getFirstImageFromVideo:video];
  264. dispatch_async(dispatch_get_main_queue(), ^{
  265. iv.image = image;
  266. });
  267. });
  268. }
  269. +(double)getTimeLenFromVideo:(NSString*)video{
  270. NSURL* url;
  271. if( [video rangeOfString:@"http://"].location == NSNotFound && [video rangeOfString:@"https://"].location == NSNotFound)
  272. url = [NSURL fileURLWithPath:video];
  273. else
  274. url = [NSURL URLWithString:video];
  275. //获取视频的首帧作为缩略图
  276. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
  277. NSParameterAssert(asset);
  278. return asset.duration.value/asset.duration.timescale;
  279. }
  280. @end