简体   繁体   中英

How to store qrcode(image) generated in the application folder

I want to store the generated qrcode to be store in my application folder. Then use it as an accessory view for a cell in UITableView.

-(void)generateQRCode
{
    NSError *err;    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

    [dict setValue:[Utility customerID] forKey:@"customerID"];
    [dict setValue:serverPath.text forKey:@"serverPath"];

    NSData *json = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&err];
    NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSISOLatin1StringEncoding];
    NSData *data = [jsonString dataUsingEncoding:NSISOLatin1StringEncoding];

    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

    [filter setValue:data forKey:@"inputMessage"];
    [filter setValue:@"Q" forKey:@"inputCorrectionLevel"];

    qrcodeImage = filter.outputImage;

    _imgQRCode.image = [UIImage imageWithCIImage:qrcodeImage];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPaths = [paths objectAtIndex:0];
    filepath = [documentPaths stringByAppendingString:@"qrcodeImage.png"];
    NSData *pngData = UIImagePNGRepresentation(_imgQRCode.image);
    [pngData writeToFile:filepath atomically:YES];
}

This func will return you path of documents dir.

func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory}

You can save your image in documents dir using this code :

if let image = UIImage(named: "example.png") {
if let data = UIImagePNGRepresentation(image) {
    let filename = getDocumentsDirectory().stringByAppendingPathComponent("copy.png")
    data.writeToFile(filename, atomically: true)
}}

After saving your image you can reload your tableview and configure the cell . Let me know if you dont know how to config accessory view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM