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

use*_*914 123 iphone camera objective-c ipad ios

我是Xcode的新手(使用4.3)并且不确定如何将图像保存到设备的相机胶卷.到目前为止我所做的一切都是为按钮保存图像设置IBAction.我可以使用哪种库方法或功能将图像保存到用户的相机胶卷?

pas*_*aya 233

你使用该UIImageWriteToSavedPhotosAlbum()功能.

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

编辑:

//ViewController.m
- (IBAction)onClickSavePhoto:(id)sender{

    UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil);
}
Run Code Online (Sandbox Code Playgroud)

  • 从iOS10开始,必须在plist中使用NSPhotoLibraryUsageDescription键。否则应用程序崩溃。 (3认同)

dig*_*und 54

这是iOS8 +使用Photos框架的答案.

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);
    }
}];
Run Code Online (Sandbox Code Playgroud)

迅速:

// 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
    }
})
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

  • 如何获取图片网址@ricardopereira (2认同)

Gra*_*rks 9

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

UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil);
Run Code Online (Sandbox Code Playgroud)

您可能希望保存视频以上传到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];
}
Run Code Online (Sandbox Code Playgroud)


Mah*_*ari 8

在 Swift 4 中将图像保存在照片库中

在 info.plist 中添加“隐私 - 照片库添加使用说明”

然后使用以下代码保存图像

UIImageWriteToSavedPhotosAlbum(imageView.image!, nil, nil, nil)
Run Code Online (Sandbox Code Playgroud)