1、C言語の関数の方法
注:UIImageWriteToSavedPhotosAlbum方法は、それ以外の場合は崩壊する、プロキシメソッドを実装する必要があります。
// パラメータ1:画像オブジェクト // パラメータ2:標的結合の成功方法 // パラメータ3:正常メソッド呼び出した後 // 情報を渡す必要性(成功したメソッド呼び出しの後にパラメータ):パラメータ4 UIImageWriteToSavedPhotosAlbum(self.imageView.imageを、自己、@selector(画像:didFinishSavingWithError:contextInfo :)はnil); の#pragmaマーク- <アルバムに保存> - (無効)画像:(UIImage *)画像didFinishSavingWithError:(NSError *)エラーcontextInfo :( 無効 * )contextInfo { NSStringの * MSG = ゼロ、 IF (エラー){ MSG = @ " 画像を保存に失敗しました" ; } 他{ MSG = @ "絵の成功を保存" ; } }
方法2:使用のフレームワークは、写真を実装しました
2.1 Photos01-基本的な理解
PHAsset:PHAssetオブジェクトが画像アルバムまたはビデオ表す
PHAssetCollectionを:PHAssetCollectionターゲットはアルバムを表します
私たちはカメラロール[]に画像を保存したい場合は、まず、これらのオブジェクトを操作する方法、PHAssetは、新しいオブジェクトを追加することを確認しなければなりませんか?何もないが、これらのオブジェクトCRUD。
PHAsset
一个PHAsset对象就代表相册中的一张图片或者一个视频
CRUD操作で写真/ビデオ関連の変更を含むPHAssetChangeRequestチャールズ [PHAsset fetchAssets ...];
PHAssetCollection
一个PHAssetCollection 对象就代表一个相册
CRUD PHAssetCollectionChangeRequestアルバムは、操作に関連するすべての変更が含まれます
検索
[PHAssetCollection fetchAssetCollectionsContainingAsset:...];
2.2お使いのカメラロールに画像を保存Photos02-
// ] [カメラロールに画像を保存 /// 非同期動作変更 [PHPhotoLibrary sharedPhotoLibrary] performChangesを^ { [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image]; } CompletionHandler: ^(BOOL成功、NSError * _Nullableエラー){ IF (エラー){ のNSLog(@ " %の@ "、"@ 失敗保存" ); } 他{ のNSLog(@ " %の@ "、"@ 正常に保存" ); } }]。
2.3 Photos03-新しいアルバムを作成します
*エラー= NSErrorのはnil; [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait: ^ { // 取得するアプリの名前 のNSString *タイトル= [NSBundle mainBundle] .infoDictionary [(__橋NSStringの* )kCFBundleNameKey]; // 作成したカスタムアルバム[] [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:表題]; }エラー:&エラー]。
2.4 Photos04-問い合わせアルバム
NSStringの*タイトル= [NSBundle mainBundle] .infoDictionary [(__ブリッジNSStringの* )kCFBundleNameKey]。 // 查询所有【自定义相册】 PHFetchResult <PHAssetCollection *> *のコレクション= [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbumサブタイプ:PHAssetCollectionSubtypeAlbumRegularオプション:nilを]; PHAssetCollection * createCollection = nilを; 用(PHAssetCollection *コレクション内のコレクション){ 場合([collection.localizedTitle isEqualToString:表題]){ createCollection = コレクション。 破ります; } } もし(createCollection == NIL){ // これは、アルバム作成アプリに対応していない // 作成したカスタムアルバム[A] NSError *誤差= ゼロ、 __blockのNSStringの * createCollectionID = ニル; [PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait: ^ { // カスタムアルバム] [作成 createCollectionID = ; .placeholderForCreatedAssetCollection.localIdentifier:タイトル[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle] }エラー:&エラーを]; createCollection =[PHAssetCollectionのfetchAssetCollectionsWithLocalIdentifiers:[createCollectionID] @オプション:nilを] .firstObject。 } のNSLog(@ " %の@ "、createCollection)。
2.5カスタムアルバムに画像を保存Photos05-
// まず[]カメラロールに画像を保存 ///は操作変更同期 NSError *誤差= nilを; __block PHObjectPlaceholder *プレースホルダ= ニル; [PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait: ^ { プレースホルダ = [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView .image] .placeholderForCreatedAsset; }エラー:&エラー]; IF (エラー){ のNSLog(@ " 失敗保存" ); 戻り値; } // 2 []カスタマイズされたアルバムを有する PHAssetCollection * assetCollection =self.createCollection; IF(assetCollection == nilを){ のNSLog("@ アルバムが失敗作成" ); } // ちょうどカメラロール]カスタムアルバムへの参照内の[画像] [に保存3. [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^ { PHAssetCollectionChangeRequest * requtes = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection]; [requtes addAssets:[プレースホルダ] @]; }エラー:&エラー]; IF (エラー){ のNSLog("@ 画像を保存できませんでした" ); } 他{ NSLog(@ " 名前を付けて保存画像を保存成功" ); }