ios相机读取图片到相册writeImageToSavesPhotosAlbum出现的问题
2018-03-19 21:14
1421 查看
读取相机选择的照片 MImaLibTool *imgLibTool = [MImaLibTool shareMImaLibTool];
[imgLibTool.lib writeImageToSavedPhotosAlbum:[theImage CGImage] orientation:(ALAssetOrientation)[theImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
} else {
[imgLibTool.lib assetForURL:assetURL resultBlock:^(ALAsset *asset) {
if (asset) {
[_arrSelected addObject:asset];
[self finishSelectImg];
[picker dismissViewControllerAnimated:NO completion:nil];
}
} failureBlock:^(NSError *error) {
}];
}
}];
在获取图片路径时,由于图片过大经常可能会导致asset为空,最近在相机拍完照片完成获取图片时在代理回调方法didFinishSavingWithError中,不能及时获取到本地相册图片库中的数据,导致卡在相机界面的问题
其实管理相册的是`ALAssetsLibrary`这个类,苹果官方对它的描述是这样的:An instance of ALAssetsLibrary provides access to the videos and photos that are under the control of the Photos application. `ALAssetsLibrary`中提供了保存到相册的API,而且苹果官方已经不推荐使用:
1 // With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.
2 - (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_0, 9_0, "Use creationRequestForAssetFromImage: on PHAssetChangeRequest from the Photos framework to create a new asset instead");
3
4 // The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image
5 - (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_1, 9_0, "Use creationRequestForAssetFromImage: on PHAssetChangeRequest from the Photos framework to create a new asset instead");
6
7 // If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten
8 - (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_1, 9_0, "Use creationRequestForAssetFromImageData: on PHAssetChangeRequest from the Photos framework to create a new asset instead")那么TZImagePicker这个处理就比较好,根据系统版本来判断比较适合去调那个方法去保存和取出相册的图片,如果是原来的方法那么就需要多给系统0.5s的时间,让系统去更新相册的数据if (iOS8Later) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
if (iOS9Later) {
NSData *data = UIImageJPEGRepresentation(image, 0.9);
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
options.shouldMoveFile = YES;
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
[request addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
} else {
PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
}
} completionHandler:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (success && completion) {
completion(nil);
} else if (error) {
NSLog(@"保存照片出错:%@",error.localizedDescription);
if (completion) {
completion(error);
}
}
});
}];
} else {
[self.assetLibrary writeImageToSavedPhotosAlbum:image.CGImage orientation:[self orientationFromImage:image] completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"保存图片失败:%@",error.localizedDescription);
if (completion) {
completion(error);
}
} else {
// 多给系统0.5秒的时间,让系统去更新相册数据
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (completion) {
completion(nil);
}
});
}
}];
}
部分图片和资料引用地址: http://www.cnblogs.com/A--G/p/5769502.html
[imgLibTool.lib writeImageToSavedPhotosAlbum:[theImage CGImage] orientation:(ALAssetOrientation)[theImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
} else {
[imgLibTool.lib assetForURL:assetURL resultBlock:^(ALAsset *asset) {
if (asset) {
[_arrSelected addObject:asset];
[self finishSelectImg];
[picker dismissViewControllerAnimated:NO completion:nil];
}
} failureBlock:^(NSError *error) {
}];
}
}];
在获取图片路径时,由于图片过大经常可能会导致asset为空,最近在相机拍完照片完成获取图片时在代理回调方法didFinishSavingWithError中,不能及时获取到本地相册图片库中的数据,导致卡在相机界面的问题
其实管理相册的是`ALAssetsLibrary`这个类,苹果官方对它的描述是这样的:An instance of ALAssetsLibrary provides access to the videos and photos that are under the control of the Photos application. `ALAssetsLibrary`中提供了保存到相册的API,而且苹果官方已经不推荐使用:
1 // With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.
2 - (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_0, 9_0, "Use creationRequestForAssetFromImage: on PHAssetChangeRequest from the Photos framework to create a new asset instead");
3
4 // The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image
5 - (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_1, 9_0, "Use creationRequestForAssetFromImage: on PHAssetChangeRequest from the Photos framework to create a new asset instead");
6
7 // If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten
8 - (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_1, 9_0, "Use creationRequestForAssetFromImageData: on PHAssetChangeRequest from the Photos framework to create a new asset instead")那么TZImagePicker这个处理就比较好,根据系统版本来判断比较适合去调那个方法去保存和取出相册的图片,如果是原来的方法那么就需要多给系统0.5s的时间,让系统去更新相册的数据if (iOS8Later) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
if (iOS9Later) {
NSData *data = UIImageJPEGRepresentation(image, 0.9);
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
options.shouldMoveFile = YES;
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
[request addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
} else {
PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
}
} completionHandler:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (success && completion) {
completion(nil);
} else if (error) {
NSLog(@"保存照片出错:%@",error.localizedDescription);
if (completion) {
completion(error);
}
}
});
}];
} else {
[self.assetLibrary writeImageToSavedPhotosAlbum:image.CGImage orientation:[self orientationFromImage:image] completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"保存图片失败:%@",error.localizedDescription);
if (completion) {
completion(error);
}
} else {
// 多给系统0.5秒的时间,让系统去更新相册数据
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (completion) {
completion(nil);
}
});
}
}];
}
部分图片和资料引用地址: http://www.cnblogs.com/A--G/p/5769502.html
相关文章推荐
- Xcode8用Swift将图片写入到相册UIImageWriteToSavedPhotosAlbum方法
- 保存图片到相册 iphone UIImageWriteToSavedPhotosAlbum 保存图片
- iOS 10 调用系统相册相机出现问题:-> 0x183a58d9c <+8>: b.lo 0x183a58db4 ; <+32>
- iphone UIImageWriteToSavedPhotosAlbum 保存图片
- UIImageWriteToSavedPhotosAlbum保存图片的方法
- UIImageWriteToSavedPhotosAlbum保存图片崩溃
- iphone UIImageWriteToSavedPhotosAlbum 保存图片
- iphone UIImageWriteToSavedPhotosAlbum 保存图片
- iphone UIImageWriteToSavedPhotosAlbum 保存图片
- iOS保存系统相册中的照片到应用沙盒中出现的问题--图片旋转90度,使用CGImageRelease造成应用崩溃
- UIImageWriteToSavedPhotosAlbum保存图片的方法
- wx.saveImageToPhotosAlbum将图片保存到本地
- Android基础--调用系统相机,打开相册,展示到界面,出现的一些问题Uri为空,data为null,图片错乱问题
- UIImageWriteToSavedPhotosAlbum保存图片的方法
- 微信小程序保存网络图片的方式2:downfile+saveImageToPhotosAlbum
- UIImageWriteToSavedPhotosAlbum保存图片的方法
- 微信小程序保存网络图片的方式1:getimageinfo+saveImageToPhotosAlbum
- UIImageWriteToSavedPhotosAlbum 保存图片
- iOS 相册图片,相机图片被旋转问题 uiimage orientation
- image文件图片显示,存储,读取问题