ios 如何将图像保存到相机胶卷?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11131050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I save an image to the camera roll?
提问by user1470914
I am new to Xcode (using 4.3) and am not sure how to save an image to the device's camera roll. All that I have done so far is set up an IBAction for the button to save the image. What library method or function can I use to save an image to the user's camera roll?
我是 Xcode 的新手(使用 4.3)并且不确定如何将图像保存到设备的相机胶卷中。到目前为止,我所做的只是为按钮设置了一个 IBAction 来保存图像。我可以使用什么库方法或函数将图像保存到用户的相机胶卷?
回答by pasawaya
You use the UIImageWriteToSavedPhotosAlbum()
function.
您使用该UIImageWriteToSavedPhotosAlbum()
功能。
//Let's say the image you want to save is in a UIImage called "imageToBeSaved"
UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil);
Edit:
编辑:
//ViewController.m
- (IBAction)onClickSavePhoto:(id)sender{
UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil);
}
回答by digitalHound
Here's an answer for iOS8+ using the Photos framework.
这是使用照片框架的 iOS8+ 的答案。
Objective-C:
目标-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:
迅速:
// 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
}
})
Here's a linkto the Apple documentation.
这是Apple 文档的链接。
Don't forget to add the appropriate key/value to your info.plist to request permission to access the photo library:
不要忘记将适当的键/值添加到您的 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>
回答by Graham Perks
For reference, you can save videos in a similar manner:
作为参考,您可以以类似的方式保存视频:
UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil);
You might want to save a video to upload to Instagram, for example:
您可能想要保存视频以上传到 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];
}
回答by Mahesh Chaudhari
Save the image in photo library in Swift 4
在 Swift 4 中将图像保存在照片库中
在 info.plist 中添加“隐私 - 照片库添加使用说明”Then use following code to save image
然后使用以下代码保存图像
UIImageWriteToSavedPhotosAlbum(imageView.image!, nil, nil, nil)
回答by user3408691
Use asImage() to get unique content to save to the camera roll.
使用 asImage() 获取要保存到相机胶卷的独特内容。
If you use asImage() you can save a variety of fun things to the camera roll with just a few lines of code! This can be very powerful if the object has some transparency already incorporated.
如果您使用 asImage() ,您只需几行代码就可以将各种有趣的东西保存到相机胶卷中!如果对象已经包含一些透明度,这可能非常强大。
asImage() works with UITextView, WKWebView, UIImageView, UIButton, UISlider, UITableView to name some objects (but they may need to be visible when you get the image (having a non zero alpha)). I even use it to capture tiling, but that's loaded into a UIImageView already in my design. I suspect asImage() may work with many more object types too, but I only tried the ones I mentioned.
asImage() 与 UITextView、WKWebView、UIImageView、UIButton、UISlider、UITableView 一起使用来命名一些对象(但当您获取图像时它们可能需要可见(具有非零 alpha))。我什至用它来捕获平铺,但它已加载到我的设计中已经存在的 UIImageView 中。我怀疑 asImage() 也可以处理更多的对象类型,但我只尝试了我提到的那些。
If it's a UITextView, and you set the background color to .clear then the text gets saved with a transparent background. If your text contains emoji or Memoji, you can now get those images into the camera roll, or into UIImageViews internal to your app. Having Memoji/Emoji with a transparent background in your camera roll where they can be used in any variety of applications is powerful.
如果它是 UITextView,并且您将背景颜色设置为 .clear,那么文本将使用透明背景保存。如果您的文本包含表情符号或拟我表情,您现在可以将这些图像放入相机胶卷或应用程序内部的 UIImageViews 中。有Memoji /绘文字与相机胶卷中的透明背景,他们可以在任何种类的应用中是强大的。
Other objects may have some transparency if you crop a photo image to a circle, or set the corner radius to clip off the corners.
如果您将照片图像裁剪为圆形,或设置角半径以剪掉角,则其他对象可能具有一定的透明度。
Note in my code, pointerToTextObjectSelected is a UITextView
注意在我的代码中,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)