iOS9图片保存到相册
2015-10-07 10:52
363 查看
[PHPhotoLibrary
requestAuthorization:^(
PHAuthorizationStatus status ) {
// To preserve the metadata, we create an asset from the JPEG NSData representation.
// Note that creating an asset from a UIImage discards the metadata.
// In iOS 9, we can use -[PHAssetCreationRequest addResourceWithType:data:options].
// In iOS 8, we save the image to a temporary file and use +[PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:].
if ( [PHAssetCreationRequest
class] ) {
[[PHPhotoLibrary
sharedPhotoLibrary]
performChanges:^{
[[PHAssetCreationRequest
creationRequestForAsset]
addResourceWithType:PHAssetResourceTypePhoto
data:imageData
options:nil];
} completionHandler:^(
BOOL success,
NSError *error ) {
if ( ! success ) {
NSLog(
@"Error occurred while saving image to photo library: %@", error );
}
}];
}
else {
// iOS8
NSString *temporaryFileName = [NSProcessInfo
processInfo].globallyUniqueString;
NSString *temporaryFilePath = [NSTemporaryDirectory()
stringByAppendingPathComponent:[temporaryFileName
stringByAppendingPathExtension:@"jpg"]];
NSURL *temporaryFileURL = [NSURL
fileURLWithPath:temporaryFilePath];
[[PHPhotoLibrary
sharedPhotoLibrary]
performChanges:^{
NSError *error =
nil;
[imageData writeToURL:temporaryFileURL
options:NSDataWritingAtomic
error:&error];
if ( error ) {
NSLog(
@"Error occured while writing image data to a temporary file: %@", error );
}
else {
[PHAssetChangeRequest
creationRequestForAssetFromImageAtFileURL:temporaryFileURL];
}
} completionHandler:^(
BOOL success,
NSError *error ) {
if ( ! success ) {
NSLog(
@"Error occurred while saving image to photo library: %@", error );
}
// Delete the temporary file.
[[NSFileManager
defaultManager]
removeItemAtURL:temporaryFileURL
error:nil];
}];
}
}];
requestAuthorization:^(
PHAuthorizationStatus status ) {
// To preserve the metadata, we create an asset from the JPEG NSData representation.
// Note that creating an asset from a UIImage discards the metadata.
// In iOS 9, we can use -[PHAssetCreationRequest addResourceWithType:data:options].
// In iOS 8, we save the image to a temporary file and use +[PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:].
if ( [PHAssetCreationRequest
class] ) {
[[PHPhotoLibrary
sharedPhotoLibrary]
performChanges:^{
[[PHAssetCreationRequest
creationRequestForAsset]
addResourceWithType:PHAssetResourceTypePhoto
data:imageData
options:nil];
} completionHandler:^(
BOOL success,
NSError *error ) {
if ( ! success ) {
NSLog(
@"Error occurred while saving image to photo library: %@", error );
}
}];
}
else {
// iOS8
NSString *temporaryFileName = [NSProcessInfo
processInfo].globallyUniqueString;
NSString *temporaryFilePath = [NSTemporaryDirectory()
stringByAppendingPathComponent:[temporaryFileName
stringByAppendingPathExtension:@"jpg"]];
NSURL *temporaryFileURL = [NSURL
fileURLWithPath:temporaryFilePath];
[[PHPhotoLibrary
sharedPhotoLibrary]
performChanges:^{
NSError *error =
nil;
[imageData writeToURL:temporaryFileURL
options:NSDataWritingAtomic
error:&error];
if ( error ) {
NSLog(
@"Error occured while writing image data to a temporary file: %@", error );
}
else {
[PHAssetChangeRequest
creationRequestForAssetFromImageAtFileURL:temporaryFileURL];
}
} completionHandler:^(
BOOL success,
NSError *error ) {
if ( ! success ) {
NSLog(
@"Error occurred while saving image to photo library: %@", error );
}
// Delete the temporary file.
[[NSFileManager
defaultManager]
removeItemAtURL:temporaryFileURL
error:nil];
}];
}
}];
相关文章推荐
- iOS开发中一些小知识
- ios:NSKeyedArchiver/NSKeyedUnarchiver(归档/解档)
- iOS版本控制git小结--yoowei
- 解析iOS内存不足时的警告以及处理过程
- iOS中日期和字符串的相互转化
- iOS学习之查看图片某点的ARGB
- iOS雨笙 多线程的原理和实现
- iOS项目问题汇总<一>
- Ios66详解之地图定位失败错误信息处理
- IOS Map
- 【IOS 开发学习总结-OC-34】★★文件 I/O——NSFileManager 管理文件和目录
- IOS推送
- 如何把设计图自动转换为iOS代码? 在线等,挺急的!
- 如何把设计图自动转换为iOS代码? 在线等,挺急的!
- IOS开发笔记-01按钮操作-10.创建应用程序中的一些细节
- IOS开发笔记-01按钮操作-09.私有扩展&IBAction
- 2015/10/6 iOS 笔记 细节 应用中常见文件
- IOS 解析JSON
- [iOS开发项目-8] 喜马拉雅电台
- [iOS开发项目-8] 查看大图