每当我尝试使用相框框架保存图片时,我的应用都会崩溃。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
_mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
_mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
PHObjectPlaceholder *assetPlaceholder = _mChangeRequest.placeholderForCreatedAsset;
}
else {
NSLog(@"write error : %@",error);
}
}];
}
crash : NSInternalInconsistencyException', reason: 'This method can only be called from inside of -[PHPhotoLibrary performChanges:completionHandler:] or -[PHPhotoLibrary performChangesAndWait:error:]'
答案 0 :(得分:37)
您需要做的就是触发创建请求。如错误所示,您只能在performChanges
块内访问更改请求。
因此,为了保存图像,您可以执行以下操作:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"Success");
}
else {
NSLog(@"write error : %@",error);
}
}];
如果您需要对新创建的资源的占位符执行某些操作,可以在同一performChanges
块内访问它:
PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
PHObjectPlaceholder *assetPlaceholder = changeRequest.placeholderForCreatedAsset;
答案 1 :(得分:3)
在Swift 3中,我这样做是为了将视频保存到库中。
if mediaType.isEqual(to: (kUTTypeMovie as NSString) as String) {
if let videoURL = info[UIImagePickerControllerMediaURL] as? URL {
PHPhotoLibrary.shared().performChanges({
_ = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL)
}, completionHandler: { (success, error) in
if success {
print("ok")
let videoData = NSData(contentsOf: videoURL)
// use videoData here if needed...
if let posterImage = self.firstFrame(videoURL: videoURL) {
self.imageView.image = posterImage
}
picker.dismiss(animated: true) { () -> Void in
}
}
else {
print(error?.localizedDescription)
}
})
}
}
答案 2 :(得分:3)
以下是使用UIImageWriteToSavedPhotosAlbum
函数编写/保存图像到照片库的代码示例:
- (void)saveImage:(UIImage *)image {
UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
if (!error) {
// saved successfully
PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHAsset *asset = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions].firstObject;
if (asset != nil) {
// here you can use asset of your image
}
} else {
NSLog(@"save image error: %@", error);
}
}
请勿忘记添加Info.plist
一个键值对Privacy - Camera Usage Description
,其中包含使用说明。
答案 3 :(得分:2)
您将在照片应用中看到该图片
你可能会更改代码......
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
__block PHAssetChangeRequest *_mChangeRequest = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
_mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
PHObjectPlaceholder *assetPlaceholder = _mChangeRequest.placeholderForCreatedAsset;
}
else {
NSLog(@"write error : %@",error);
}
}];
}