我怎么能保存到相机胶卷的图像?我怎么能保存到相机胶卷的图像?(How can I save an i

2019-06-02 15:19发布

我是新来的Xcode(使用4.3),我不知道如何将图像保存到设备的相机胶卷。 所有这一切我迄今所做的就是建立一个IBAction为为按钮保存图像。 什么库方法或功能,我可以使用,将图像保存到用户的相机胶卷?

Answer 1:

您可以使用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);
}


Answer 2:

下面是一个使用照片框架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 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
    }
})

这里有一个链接到苹果的文档。

不要忘了适当的键/值添加到您的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>


Answer 3:

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

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];
}


文章来源: How can I save an image to the camera roll?