Store images in to sqlite database

后端 未结 1 1460
情书的邮戳
情书的邮戳 2020-12-18 14:59

I know this question was asked by many people and there are several discussions and arguments on \"Why to use sqlite,use FMDB,core data\" etc.. to store or insert images in

相关标签:
1条回答
  • 2020-12-18 15:13

    For saving image to documents

    - (void)saveImage:(UIImage *)image forPerson:(NSString *)fullName  {
        //  Make file name first
        NSString *filename = [fullName stringByAppendingString:@".png"]; // or .jpg
    
        //  Get the path of the app documents directory
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        //  Append the filename and get the full image path
        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:filename];
    
        //  Now convert the image to PNG/JPEG and write it to the image path
        NSData *imageData = UIImagePNGRepresentation(image);
        [imageData writeToFile:savedImagePath atomically:NO];   
    
        //  Here you save the savedImagePath to your DB
        ...
    }
    

    So, you later can get the image by

    - (UIImage *)loadImage:(NSString *)filePath  {
        return [UIImage imageWithContentsOfFile:filePath];
    }
    
    0 讨论(0)
提交回复
热议问题