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

时间:2017-01-07 07:20:01

标签: ios iphone xcode camera save

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

3 个答案:

答案 0 :(得分:1)

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

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。我希望,这就是你想要的。

答案 1 :(得分:1)

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

//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;

    }

答案 2 :(得分:1)

您可以使用自定义名称存储图像,如下所示:

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