我刚接触iPhone,我想将图像保存到相册中,并且想要该图像路径。
我已经做到了。
现在,我要为图像自定义名称并使用该名称存储图像。
我使用了下面的代码,但没有将任何图像放入相册。
谁能指导我我在哪里错了?
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
// Todo to use custom name comment this line.
//UIImageWriteToSavedPhotosAlbum(self.imageView.image,nil,nil,nil);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
});
}
最佳答案
您可以使用以下自定义名称存储图像:
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//save image in Document Derectory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Get Path : %@",documentsDirectory);
//create Folder if Not Exist
NSError *error = nil;
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSString *customePhotoName=@"MyPhotoName";
NSString* path= [dataPath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",customePhotoName]];
NSData* imageData = UIImagePNGRepresentation(chosenImage);
[imageData writeToFile:path atomically:YES];
NSLog(@"Save Image Path : %@",path);
关于ios - 保存自定义相机捕获图像iOS Xcode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41518946/