保存自定义相机捕获图像iOS Xcode



我是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);

在我的一个项目中,我做了这个:

let directory = "yourDirectoryName"
let fileName = "file.png"
let directoryPath = NSHomeDirectory() + "/Library/Caches/(directory)"
do {
    try NSFileManager.defaultManager().createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)
    UIImagePNGRepresentation(image)?.writeToFile(directoryPath+fileName), atomically: true)
} catch let error as NSError {}

代码在 Swift 2.3 中工作。我希望,这就是你想要的。

验证 NSData 和 UIImage 对象是否正确,将"原子"设置为 YES,请查看我使用的以下代码:

//Save your image with a completion selector
 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
 //Completion selector
- (void) image: (UIImage *) image
 didFinishSavingWithError: (NSError *) error
    contextInfo: (void *) contextInfo 
   {
       if(error)
     {
    NSLog(@"save error :%@", [error localizedDescription]);
     }
      else if (!error)
     {
            PHAsset *asset = [self fetchImageAsset];
     }
 }
    //Fetch image based on create date or whatever naming system you follow
- (nullable PHAsset *)fetchImageAsset
 {
        PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
        fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
        PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
        PHAsset *lastAsset = [fetchResult lastObject];
        return lastAsset;
    }

最新更新