I use this function to install betch, but I ran into the resulting Waring memory. I am going to save the local image gallery image path identifier in the main data. Below I described how I get all the images from the iPhone gallery and save them in the master data. Please give me a helpful 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)
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 = 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()
}
}
}
source
share