4

I am using this function for betch insertion but i am facing received memory Waring issue. I am going to save local identifier of image path of image gallery into core data . I have described below by function how i am getting all images from iPhone gallery and saved it into core data . Please give me useful suggestion .

func getAllImageFromIphoneGallery() {

let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
self.assetsFetchResult  = PHAsset.fetchAssetsWithMediaType(.Image, options: allPhotosOptions)
let manager = PHImageManager.defaultManager()
self.assetsFetchResult.enumerateObjectsUsingBlock{(object: AnyObject!,
   count: Int,
     stop: UnsafeMutablePointer<ObjCBool>) in

      if object is PHAsset{
       let asset = object as! PHAsset
       let imageSize = CGSize(width: asset.pixelWidth,
       height: asset.pixelHeight)
      /* For faster performance, and maybe degraded image */
       let options = PHImageRequestOptions()
       options.deliveryMode = .FastFormat
       options.synchronous = true
       manager.requestImageForAsset(asset,
       targetSize: imageSize,
       contentMode: .AspectFill,
       options: options,
       resultHandler: {
       (image, info) -> Void in
       debugPrint(object.localIdentifier)

       if   let imageURL : NSURL? = info!["PHImageFileURLKey"] as AnyObject? as? NSURL
       {
        if imageURL != nil{
        //  let urlString: String = imageURL!.path!
        let urlString: String = object.localIdentifier


         print("this is image path \(urlString)")
         self.arrPhotoPath.addObject(urlString)

         }
       }
     }           
    )
  }
 }
}


func createTask() {

    let context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
    context.persistentStoreCoordinator = (UIApplication.sharedApplication().delegate as! AppDelegate).persistentStoreCoordinator

    context.performBlock {
        while(true) { //
            autoreleasepool {

                let entityDescripition = NSEntityDescription.entityForName("PhotoLibrary", inManagedObjectContext: self.managedObjectContext)
                debugPrint(self.arrPhotoPath);
                let array = self.arrPhotoPath
                if (array.count > 0)
                {
                    for item in array {
                        let task = Tasks(entity: entityDescripition!, insertIntoManagedObjectContext: self.managedObjectContext)
                        task.frameid = "1"
                        task.photopath = item as! String
                        task.accesstoken = ""
                        task.photoname = "abc"
                        task.deliverystatus = "no"
                        array.removeObject(item)
                    }
                }

            }
            do {
                try self.managedObjectContext.save()
            } catch _ {
            }
            self.managedObjectContext.reset()
        }
    }
}
Amit Srivastava
  • 1,105
  • 9
  • 18
IOSDev
  • 680
  • 3
  • 17
  • 2
    Have you profiled the code to see exactly what is causing your memory issues? Have you considered another autorelease in your for loop of create task? – Jeef Oct 26 '16 at 10:19
  • Check this http://stackoverflow.com/questions/12295415/core-data-memory-usage-and-memory-warning – Manishankar Oct 26 '16 at 10:25
  • I am going to store image local identifier in large amount of data that's why it is giving received memory warning, Please suggest me what should i do ? – IOSDev Oct 26 '16 at 10:45

0 Answers0