我目前正在通过调用在后台线程中将一些文件写入磁盘
dispatch_async(my_queue,^{
[self writeToRoot:filename data:data];
};
- (BOOL)writeToRoot:(NSString *)path data:(NSData *)content
{
NSString *fullPath = [[self rootPath] stringByAppendingPathComponent:path];
NSString *pathWithoutFile = [fullPath stringByDeletingLastPathComponent];
BOOL directoryExists = [[NSFileManager defaultManager] fileExistsAtPath:pathWithoutFile];
if (!directoryExists) {
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:pathWithoutFile
withIntermediateDirectories:YES
attributes:nil error:&error];
NSParameterAssert(error == nil);
}
return [content writeToFile:fullPath atomically:NO];
}
Run Code Online (Sandbox Code Playgroud)
我这样做,因为它不会阻止主线程.我的问题是如何确保线程安全.在执行此后台操作时,当我尝试通过调用从磁盘读取文件时会发生什么:
[NSData dataWithContentsOfFile:fullPath];
Run Code Online (Sandbox Code Playgroud)
内容会被破坏吗?或者写操作会锁定文件,读操作会等到写完成吗?
我的相册里有一张gif图片.当我使用它UIImagePickerController来选择该图像时,我需要将图像转换NSData为存储.
早些时候,我用过
NSData *thumbData = UIImageJPEGRepresentation(thumbnail, 0.5);
Run Code Online (Sandbox Code Playgroud)
但它不适用于gif图像.thumbData将是零.
我如何NSData从gif图像中获取?
我怎么知道它是一个需要特殊处理的gif图像?
我试图在用户选择图像后从UIImagePickerController推送视图控制器(用于额外编辑).
它被成功推动了.但是当弹出该视图控制器并且用户返回编辑屏幕时,编辑屏幕不再可交互.有谁知道这是什么问题.
-(void)didTapBtn:(id)sender
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker.delegate = self;
self.picker.allowsEditing = YES;
[self presentViewController:self.picker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (image == nil) {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if (image == nil) {
return;
}
self.test = [[BTTestViewController alloc] init];
self.test.delegate = self;
[self.picker pushViewController:self.test animated:YES];
}
//Called by BTTestViewController
-(void)didCancel
{
[self.picker popViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)