用OC写一个字符串写入文件方法
时间: 2023-03-14 08:23:59 浏览: 91
答:要用OC写一个字符串写入文件,可以使用NSString的writeToFile:atomically:encoding:error:方法,例如:NSString *str = @"Hello World"; [str writeToFile:@"/tmp/hello_world.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
相关问题
OC 中怎么判断一个字符串为空
在 Objective-C 中,可以使用以下代码判断一个字符串是否为空:
```
if (string.length == 0) {
// 字符串为空
}
```
其中,`string` 是要判断的字符串对象,`length` 是字符串的长度属性。如果字符串的长度为 0,则可以判断该字符串为空。
使用oc写一个视频压缩工具方法
下面是一个使用 AVAssetExportSession 类来压缩视频的示例代码:
```
- (void)compressVideo:(NSURL *)inputURL outputURL:(NSURL *)outputURL {
AVAsset *asset = [AVAsset assetWithURL:inputURL];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusCompleted:
NSLog(@"视频压缩成功");
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"视频压缩失败: %@", exportSession.error);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"视频压缩取消");
break;
default:
break;
}
}];
}
```
这个方法接受两个参数:输入视频的 URL 和输出视频的 URL。使用 AVAsset 类加载输入视频,然后使用 AVAssetExportSession 类进行压缩。在这个示例中,我们使用了 AVAssetExportPresetMediumQuality,这是一个中等质量的压缩预设。输出文件类型为 AVFileTypeMPEG4。shouldOptimizeForNetworkUse 属性设置为 YES,以便在网络传输时优化压缩。最后,使用 exportAsynchronouslyWithCompletionHandler 方法开始压缩并在完成时处理结果。