将照片库中的图像保存到应用程序Xcode中的主程序包中

时间:2016-10-04 17:41:38

标签: xcode ios7 photolibrary mainbundle

是否可以将我从照片库中挑选的图像保存到我应用的主套装中?我以为我读到了一些你无法写入Xcode中应用程序主包的地方。如果这不是真的,那么我怎么可能去做呢。

1 个答案:

答案 0 :(得分:0)

Use this code to save image from photo Library to Main Bundle

- (void)saveImage:(UIImage*)image withImageName:(NSString*)imageName {
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
}