美文网首页
开发知识点识记

开发知识点识记

作者: rgcyc | 来源:发表于2017-04-02 14:22 被阅读49次

iOS 10 保存视频到相册 crash

控制台有如下输出信息:

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

解决方法:

info.plist 文件中添加配置项,这里我希望写文件至相册中,故添加如下配置:

<key>NSPhotoLibraryUsageDescription</key>
<string>此 App 需要您的同意才能读取媒体资料库</string>

如需访问摄像头、通讯录和麦克风权限,同样需要在 info.plist 文件中配置:

<key>NSCameraUsageDescription</key>    
<string>cameraDesciption</string>

<key>NSContactsUsageDescription</key>    
<string>contactsDesciption</string>

<key>NSMicrophoneUsageDescription</key>    
<string>microphoneDesciption</string>

如记不住这些 key 值,将 info.plist 文件以 Property List 的方式打开,然后添加 Item,输入 Privacy 可以定位到这些权限。

iOS 9 Http 访问

同上例,打开 info.plist 文件,添加 Item,输入 App Transport Security Settings,此时会在 info.plist 中添加类型为字典的 Item,继续在字典中添加 Item,Key 为 Allow Arbitrary Loads, Value 为 Yes

NSURLSession 下载文件

直接下载,不实时监测下载进度

- (void)downloadTask1{
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/5.png"];
    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url
 completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

     // location:下载好的文件的路径(tmp 目录)
     // response:建议文件名称等
     // session的优点:不会使内存爆掉,系统会边下载边写到沙盒的temp中.
     // 因为temp中的数据随时会被清除(可能刚写入就被删除),所以要将数据移动/拷贝到caches中.
     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
     NSString *fullPath = [caches stringByAppendingPathComponent:response.suggestedFilename];

     NSFileManager *manager = [NSFileManager defaultManager];
     [manager moveItemAtPath:location.path toPath:fullPath error:nil];
 }];

    //启动任务
    [downloadTask resume];
}

如需监听下载进度,需实现 NSURLSessionDelegate 中的三个方法,并且 Task 需按照下面的方式创建:

NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];

如采用带 callback 的方式创建,Delegate 中的三个回调函数不会被回调。

NSURLSession的简单使用及下载

保存视频至相册

函数 void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, id completionTarget, SEL completionSelector, void * contextInfo); 可将 videoPath 中的视频保存到相册中,设置 completionSelector 可以接收保存结果的回调。

- (void)saveVideoToAlbum:(NSString *)path {
    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path)) {
        UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
    }
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"error - %@", error);
    } else {
        NSLog(@"保存成功");
    }
}

如需保存沙盒中图片至相册,可以调用方法 void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void * contextInfo);

如项目中需要上传、下载 LivePhoto,可以参考PHAsset 中的图片和视频文件

使用正则表达式从复杂字符串中截取内容

使用正则表达式截取内容一般步骤:

  • 定义正则表达式模式字符串 pattern,将需要截取的内容用小括号()括起来;
  • 使用 pattern 来初始化 NSRegularExpression;
  • 调用 - (NSArray<NSTextCheckingResult *> *)matchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range 查找匹配的字符串;
NSString *pattern = @"<video id=\"video-player\" src=\"(.*?)\"></video>";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];

if (!error) {
   NSArray *matches = [regex matchesInString:htmlStr options:0 range:NSMakeRange(0, htmlStr.length)];
   NSString *matchedStr = nil;
   for (NSTextCheckingResult *match in matches) {
       NSRange range = [match range];
       matchedStr = [htmlStr substringWithRange:range];
   }
}

相关文章

  • 开发知识点识记

    iOS 10 保存视频到相册 crash 控制台有如下输出信息: This app has crashed bec...

  • 第六章 绩效考评与管理

    考核知识点与考核目标 (一) 绩效的基本内涵 识记:绩效的概念 绩效的特点 (二) 绩效考评和绩效管理 识记:绩效...

  • 第六章 绩效考评与管理

    考核知识点与考核目标 (一) 绩效的基本内涵 识记:绩效的概念 绩效的特点 (二) 绩效考评和绩效管理 识记:绩效...

  • 心理课第七次打卡~虎雅娜

    6一、本节课印象最深刻的三个知识点。 1.无意识记和有意识记,有意记忆与机械记忆,以及记忆表现。 2.ps...

  • 测试开发知识点(三)

    传送门 测试开发知识点(一)测试开发知识点(二)测试开发知识点(三)测试开发知识点(四)测试开发知识点(五) 自动...

  • 测试开发知识点(一)

    传送门 测试开发知识点(一)测试开发知识点(二)测试开发知识点(三)测试开发知识点(四)测试开发知识点(五) 软件...

  • 测试开发知识点(二)

    传送门 测试开发知识点(一)测试开发知识点(二)测试开发知识点(三)测试开发知识点(四)测试开发知识点(五) We...

  • 面试被问到的问题

    传送门测试开发知识点(一)测试开发知识点(二)测试开发知识点(三)测试开发知识点(四)测试开发知识点(五) 1、请...

  • 需要识记的知识点

    1.固定资产、无形资产、长期股权投资、投资性房地产、商誉等其他资产以可收回金额为比较基础计提减值,减值不可以转回;...

  • 第五章 员工的培训与开发方法

    考试大纲 (一)员工培训与开发方法概述 识记:员工培训与开发的基本内涵 理解:员工培训与开发的意义 应用:员工培训...

网友评论

      本文标题:开发知识点识记

      本文链接:https://www.haomeiwen.com/subject/jmjgottx.html