0

是否可以将我从照片库中选择的图像保存到我的应用程序的主包中?我以为我在某个地方读到了您无法在 Xcode 中写入应用程序的主包的地方。如果这不是真的,我将如何去做。

4

1 回答 1

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)
}
于 2016-10-13T12:46:53.303 回答