Answers:
您使用该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);
}
UIImageView *imageToBeSaved;
了我的.h文件。我以为我必须@synthesize imageToBeSaved
在.m中添加,但出现错误,提示“属性实现声明缺少上下文”
这是使用Photos框架的iOS8 +的答案。
目标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 4.0
import Photos
let snapshot: UIImage = someImage
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文档的链接。
不要忘记将适当的键/值添加到info.plist来请求访问照片库的权限:
<key>NSCameraUsageDescription</key>
<string>Enable camera access to take photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Enable photo library access to select a photo from your library.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Enable photo library access to save images to your photo library directly from the app.</string>
作为参考,您可以用类似的方式保存视频:
UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil);
您可能想要保存视频以上传到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];
}
然后使用以下代码保存图像
UIImageWriteToSavedPhotosAlbum(imageView.image!, nil, nil, nil)
使用asImage()获取要保存到相机胶卷的唯一内容。
如果使用asImage(),只需几行代码就可以将各种有趣的东西保存到相机胶卷中!如果对象已包含一些透明度,则此功能非常强大。
asImage()与UITextView,WKWebView,UIImageView,UIButton,UISlider,UITableView一起使用以命名某些对象(但是在获取图像时(它们的alpha值必须为非零)可能需要可见)。我什至用它来捕获平铺,但是已经将其加载到我的设计中的UIImageView中。我怀疑asImage()可能也可以用于更多对象类型,但是我只尝试了我提到的对象类型。
如果是UITextView,并且将背景色设置为.clear,则文本将以透明背景保存。如果您的文字包含表情符号或Memoji,现在您可以将这些图像放入相机胶卷或应用程序内部的UIImageViews中。在您的相机胶卷中放置具有透明背景的Memoji / Emoji,可以在各种应用程序中使用它们。
如果将照片图像裁剪为圆形,或者将角半径设置为修剪角,则其他对象可能具有一定的透明度。
注意在我的代码中,pointerToTextObjectSelected是一个UITextView
var pointerToTextObjectSelected = UITextView()
// above populated elsewhere
let thisObject = pointerToTextObjectSelected.asImage()
let imageData = thisObject.pngData()
let imageToSave = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(imageToSave!, nil, nil, nil)