保存和读取图像数组

时间:2016-07-01 21:02:18

标签: ios swift core-data swift2 nsuserdefaults

我想保存我的图像阵列' DtaAryVar'在NSUserDefaults'或在'文件目录'或核心数据'。任何人都可以一步一步地告诉我什么是最好和最简单的方式存储它也' UIImage'数组(包含20个图像)到' NSData'转换。我的DtaAryVar'包含20张图片。我试过下面的方法,我从堆栈溢出但发现错误。请帮帮我。

var DtaAryVar: [NSData]()
 convert array of NSData to Data and store

let DtaZipVar = NSKeyedArchiver.archivedDataWithRootObject(DtaAryVar)

let DftSdbVal = NSUserDefaults.standardUserDefaults()
DftSdbVal.setObject(DtaZipVar, forKey: "ImageKey")

get back data by

var DtaZipVar = DftSdbVal.valueForKey("ImageKey")
var DtaAryVar = NSKeyedUnarchiver.unarchiveObjectWithData(DtaZipVar) as! [NSData]

let ImjAryVar = [UIImage]()
for DtaIdxVar in DtaAryVar
{
    ImjAryVar.append(UIImage(data : DtaIdxVar))
}
  

错误   无法转换类型' AnyObject的值?'预期参数类型' NSData。

enter image description here

1 个答案:

答案 0 :(得分:0)

  

| => U只能在userDefautls中存储NSData,或者不能在[NSData]

中存储coreData      

| =>如果存储在核心数据中,请将属性类型指定为二进制

| * |将原始数据中的行添加到CoreData:

let NewRowDtaVar = NSEntityDescription.insertNewObjectForEntityForName("NamTbl", inManagedObjectContext: ContxtVar)
NewRowDtaVar.setValue(ImgDtaVar, forKey: "NamKol")
AppDgtVar.saveContext()

| * |从CoreData获取行数据:

let GetDtaAryReqVar = NSFetchRequest(entityName: "NamTbl")
GetDtaAryReqVar.returnsObjectsAsFaults = false
GetDtaAryReqVar.predicate = NSPredicate(format: "NamKol = TstTxt")

let ImgDtaVar = NSData()
do
{
    let ReqResAryVar: Array  = try ContxtVar.executeFetchRequest(GetDtaAryReqVar)

    if(ReqResAryVar.count > 0)
    {
        for ReqResVar in ReqResAryVar
        {
            ImgDtaVar = ReqResVar.valueForKey("NamKol") as! NSData!
        }
    }
}
catch let ErrVar as NSError
{
    print("Fetch failed: \(ErrVar.localizedDescription)")
}

在任何viewController类之外添加以下扩展函数,并相应地尝试

extension UIImage
{

//|*| Convert Image NSData Arry to UIImage Array

    func CnvDtaAryTooImjAryFnc(ImjDtaAryPsgVar: [NSData])-> [UIImage]
    {
        var ImjAryVar = [UIImage]()
        for DtaIdxVar in ImjDtaAryPsgVar
        {
            ImjAryVar.append(UIImage(data : DtaIdxVar)!)
        }
        return ImjAryVar
    }

//|*| Convert Image NSData to UIImage Array

    func CnvDtaTooImjAryFnc(ImjDtaPsgVar: NSData)-> [UIImage]
    {
        let ImjAryVar = NSKeyedUnarchiver.unarchiveObjectWithData(ImjDtaPsgVar) as! [NSData]
        return CnvDtaAryTooImjAryFnc(ImjAryVar)
    }

//|*| Convert UIImage Array to Image NSData Arry  

    func CnvImjAryTooDtaAryFnc(ImjDtaPsgVar: [UIImage], ImjTyp ImjTypPsgVar: String?="Jpg")-> [NSData]
    {
        var ImjDtaAryVar = [NSData]()
        for DtaIdxVar in ImjDtaPsgVar
        {
            if(ImjTypPsgVar == "Png")
            {
                if let GetImjDtaVar = UIImagePNGRepresentation(DtaIdxVar)
                {
                    ImjDtaAryVar.append(GetImjDtaVar)
                }
            }
            else
            {
                if let GetImjDtaVar = UIImageJPEGRepresentation(DtaIdxVar, 1)
                {
                    ImjDtaAryVar.append(GetImjDtaVar)
                }
            }
        }
        return ImjDtaAryVar
    }

//|*| Convert UIImage Array to Image NSData 

    func CnvImjAryTooDtaFnc(ImjDtaPsgVar: [UIImage], ImjTyp ImjTypPsgVar: String?="Jpg")-> NSData
    {
        let ImjDtaAryVar = CnvImjAryTooDtaAryFnc(ImjDtaPsgVar, ImjTyp : ImjTypPsgVar)
        return NSKeyedArchiver.archivedDataWithRootObject(ImjDtaAryVar)
    }
}