从相机/图库中拍摄照片并将其保存在核心数据中

时间:2015-11-21 08:57:39

标签: ios objective-c

我有代码可以通过相机或手机画廊拍照, 但是我想将图像保存在我觉得很难的核心数据中。 我读了很多关于它的内容,我不确定图像是字符串还是二进制数据 以及如何保存它以及如何获得它。

@property (strong) NSMutableArray *allPic;
@property (strong) NSManagedObject *Image;
@end
@implementation ViewController



-(NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self     managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Picture"];
    self.allPic = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}


-(void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takePic:(id)sender {

    // ALERT SHEET.
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    //CAMERA
    UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:@"צלם" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
    {
        // If device has no camera.
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIAlertController *alertNoCamera = [UIAlertController alertControllerWithTitle:@"Error" message:@"Device has no camera" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];
            [alertNoCamera addAction:ok];
            [self presentViewController:alertNoCamera animated:YES completion:nil];
        }
        else// if  have a camera.
        {
           UIImagePickerController *picker = [[UIImagePickerController alloc] init];
           picker.delegate = self;
           picker.allowsEditing = YES;
           picker.sourceType = UIImagePickerControllerSourceTypeCamera;


           [self presentViewController:picker animated:YES completion:NULL];
        }
    }];
    // GALLERY
    UIAlertAction *openGallery = [UIAlertAction actionWithTitle:@"גלריה" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:NULL];
    }];

    [alert addAction:openCamrea];
    [alert addAction:openGallery];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     //save image

{

2 个答案:

答案 0 :(得分:0)

我认为正确的方法是将目录中的文件保存并存储到CoreData的路径。以下是我如何实现它:

{{1}}

答案 1 :(得分:0)

要在核心数据中保存图像:

您可以使用二进制数据属性类型将图像存储在Core Data中。但是你应该知道一些事情:

始终将您的UIImage转换为便携式数据格式,例如png或jpg,例如:

<meta charset="utf-8">...  

在此属性

上启用“允许外部存储”

JS Fiddle 2

核心数据会在达到某个阈值时将数据移动到外部文件。此文件也完全由Core Data管理,因此您不必担心它。

如果遇到性能问题,请尝试将二进制数据属性移动到单独的实体。

您应该将转化抽象为NSData *imageData = UIImagePNGRepresentation(image); 子类界面后面的NSData,这样您就不必担心从NSManagedObjectUIImage的转换反之亦然。

如果您的图片与模型中的实体没有很强的相关性,我建议将它们存储在Core Data之外。

从图库或相机拍摄图像:

NSData

希望这有帮助。