我需要做什么才能将我的程序生成的图像(可能是相机,可能不是)保存到iPhone上的系统照片库?
你可以使用这个功能:
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
如果要在保存完成时收到通知,则只需要completionTarget,completionSelector和contextInfoUIImage
,否则可以传入nil
.
请参阅官方文档UIImageWriteToSavedPhotosAlbum()
.
在iOS 9.0中不推荐使用.
使用iOS 4.0+ AssetsLibrary框架,使用UIImageWriteToSavedPhotosAlbum方法可以做得更快
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ if (error) { // TODO: error handling } else { // TODO: success handling } }]; [library release];
最简单的方法是:
UIImageWriteToSavedPhotosAlbum(myUIImage, nil, nil, nil);
对于Swift
,您可以参考使用swift保存到iOS照片库
要记住的一件事:如果您使用回调,请确保您的选择符符合以下形式:
- (void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo;
否则,您将崩溃,例如以下错误:
[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1]
只需将数组中的图像传递给它即可
-(void) saveMePlease { //Loop through the array here for (int i=0:i<[arrayOfPhotos count]:i++){ NSString *file = [arrayOfPhotos objectAtIndex:i]; NSString *path = [get the path of the image like you would in DOCS FOLDER or whatever]; NSString *imagePath = [path stringByAppendingString:file]; UIImage *image = [[[UIImage alloc] initWithContentsOfFile:imagePath]autorelease]; //Now it will do this for each photo in the array UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); } }
抱歉打字错误有点刚刚做了这个,但你明白了