IT

카메라 롤에 이미지를 저장해야합니까?

lottoking 2020. 7. 10. 07:44
반응형

카메라 롤에 이미지를 저장해야합니까?


Xcode (4.3 사용)를 처음 사용하고 장치의 카메라 롤에 이미지를 저장하는 방법을 잘 모르겠습니다. 내가 지금까지 한 모든 것은 이미지를 저장하는 버튼에 대한 IBAction을 설정하는 것입니다. 이미지를 사용자의 카메라 롤에 저장하기 위해 어떤 라이브러리를 사용할 수 있습니까?


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

사진 프레임 워크를 사용하는 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 설명서에 대한 링크 입니다.

사진 라이브러리에 액세스 할 수있는 권한을 요청 구매 정보 .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];
}

Swift 4의 사진 라이브러리에 이미지 저장

info.plist에서 "개인 정보 보호-사진 라이브러리 추가 사용법 설명"추가

그런 다음 다음 코드를 사용하여 이미지를 저장하십시오.

UIImageWriteToSavedPhotosAlbum(imageView.image!, nil, nil, nil)

참고 URL : https://stackoverflow.com/questions/11131050/how-can-i-save-an-image-to-the-camera-roll

반응형