2012-06-21 144 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

相关问题