如何将图像保存到相机胶卷?

我是Xcode的新手(使用4.3),我不知道如何将图像保存到设备的相机胶卷。 到目前为止,我所做的一切都是为button设置一个IBAction来保存图像。 我可以使用什么库方法或function将图像保存到用户的相机胶卷?

您使用UIImageWriteToSavedPhotosAlbum()函数。

 //Let's say the image you want to save is in a UIImage called "imageToBeSaved" UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil); 

编辑:

 //ViewController.m - (IBAction)onClickSavePhoto:(id)sender{ UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil); } 

以下是使用Photos框架的iOS8的答案。

Objective-C的:

 #import <Photos/Photos.h> UIImage *snapshot = self.myImageView.image; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:snapshot]; changeRequest.creationDate = [NSDate date]; } completionHandler:^(BOOL success, NSError *error) { if (success) { NSLog(@"successfully saved"); } else { NSLog(@"error saving to photos: %@", error); } }]; 

迅速:

 // Swift 3.0 import Photos PHPhotoLibrary.shared().performChanges({ PHAssetChangeRequest.creationRequestForAsset(from: snapshot) }, completionHandler: { success, error in if success { // Saved successfully! } else if let error = error { // Save photo failed with error } else { // Save photo failed with no error } }) 

这是Apple文档的链接 。

作为参考,您可以用类似的方式保存video:

 UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil); 

您可能想要保存一个video上传到Instagram,例如:

 // Save video to camera roll; we can share to Instagram from there. -(void)didTapShareToInstagram:(id)sender { UISaveVideoAtPathToSavedPhotosAlbum(self.videoPath, self, @selector(video:didFinishSavingWithError:contextInfo:), (void*)CFBridgingRetain(@{@"caption" : caption})); } - (void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfoPtr { NSDictionary *contextInfo = CFBridgingRelease(contextInfoPtr); NSString *caption = contextInfo[@"caption"]; NSString *escapedString = [videoPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; // urlencodedString NSString *escapedCaption = [caption stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; // urlencodedString NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@", escapedString, escapedCaption]]; [[UIApplication sharedApplication] openURL:instagramURL]; }