2012-06-21 65 views
111

我是Xcode的新手(使用4.3),我不確定如何將圖像保存到設備的相機膠捲。到目前爲止,我所做的一切都是爲按鈕設置一個IBAction來保存圖像。我可以使用什麼庫方法或功能將圖像保存到用戶的相機膠捲上?如何將圖像保存到相機膠捲?

回答

212

您使用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); 
} 
+0

假設我有' - (IBAction)onClickSavePhoto:(id)sender;'在我的ViewController.h文件中,使用上面的內容,我需要在ViewController.m文件中放置什麼?再次感謝! – user1470914

+1

@ user1470914看我的編輯。另外,您將圖像保存到相機膠捲的哪個位置?它是否已經在您的應用程序包中,或用戶是否拍攝了照片,然後將其保存到相機膠捲? – pasawaya

+0

感謝您的耐心qegal。直到現在我還沒有機會重新討論這個問題。你的編輯我們超級有用。該圖像位於應用程序的包中。應用中有幾張圖片,可以將它們視爲小縮略圖。當選擇一個小縮略圖時,它會進入一個新的視圖,並且可以查看全屏版本。我在頂部導航欄右上角有一個按鈕,其中顯示「保存」,這是我希望將圖像保存到設備的相機卷的按鈕,以便它可以作爲牆紙應用。再次感謝您的幫助和耐心(不知道爲什麼casperOne關閉了這個)。 – user1470914

6

僅供參考,您可以保存以類似的方式視頻:

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

這是一個答案f或者使用Photos框架的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 3.0 
import Photos 

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

這裏有一個link蘋果文檔。

+0

怎樣才能得到圖像網址@ricardopereira –

+0

@Mansuu請看看https://stackoverflow.com/a/372​​48867/3641812 – ricardopereira

相關問題