我希望有人能帮助我,我已经搜索过Stackoverflow和Google,但我找不到正确的解决方案。我有一个非常简单的应用程序,它可以拍摄照片(通过UIImagePickerController使用标准的iOS相机),然后将其以非常低的分辨率保存到文件系统中-let thumbNailData = UIImageJPEGRepresentation(image, 0.02)
之后,我将使用Core Data在集合视图中显示图像-仅将文件名保存在核心数据中,而不是图像中,图像仅在文件系统中。因此,但是,当我运行该应用程序时,它显示的内存使用量不超过15 MB,进程约为1-2%。一切运行正常,但添加6-7张照片后,出现奇怪的错误,例如“内存警告”,与我的iPhone的连接断开等:
Communications error: <OS_xpc_error: <error: 0x198adfa80> { count = 1, contents =
"XPCErrorDescription" => <string: 0x198adfe78> { length = 22, contents = "Connection interrupted"
所以,我真的很卡住,因为我认为我做的很轻巧,然后出现了这些错误....我已经向App Store提交了一个笔记应用程序,该应用程序的功能比该应用程序高得多,但该应用程序运行非常稳定...
有任何想法吗?
这是我的一些代码://这里,我将图片名称加载到一个数组中以在集合视图中显示
func loadColl () {
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext!
let fetchRequest:NSFetchRequest = NSFetchRequest(entityName: "PictureNote")
var error:NSError?
var result = context.executeFetchRequest(fetchRequest, error: &error) as [PictureNote]
for res in result {
println(res.latitude)
println(res.longitude)
println(res.longDate)
println(res.month)
println(res.year)
println(res.text)
pictures.append(res.thumbnail)
}
}
//这是要在集合视图中显示的代码
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let myImageCell:ImageCell = myCollectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as ImageCell
myImageCell.imageCell.image = self.loadImageFromPath(fileInDocumentsDirectory(self.pictures[indexPath.row]))
return myImageCell
}
//这是从磁盘加载图片的代码
func loadImageFromPath(path: String) -> UIImage {
let image = UIImage(contentsOfFile: path)
if image == nil {
self.newAlert("Error loading your image, try again", title: "Notice")
}
return image!
}
//这是我的保存代码
func saveNewEntry () {
var unique = NSUUID().UUIDString
var imageTitle = "Picture\(unique).jpg"
var image = self.pickedImageView.image
var path = fileInDocumentsDirectory(imageTitle)
var thumbImageTitle = "ThumbPicture\(unique).jpg"
var thumbPath = fileInDocumentsDirectory(thumbImageTitle)
if self.saveImage(image!, path: path) == true && self.saveThumbImage(image!, thumbPath: thumbPath) == true {
// Create the saving context
let context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
let entityOne = NSEntityDescription.entityForName("PictureNote", inManagedObjectContext: context)
let thisTask = PictureNote(entity: entityOne!, insertIntoManagedObjectContext: context)
// Get all the values here
self.getMyLocation()
var theDate = NSDate()
thisTask.month = Date.toString(date: theDate, format: "MM")
thisTask.year = Date.toString(date: theDate, format: "yyyy")
thisTask.longDate = theDate
thisTask.longitude = getMyLocation()[1]
thisTask.latitude = getMyLocation()[0]
thisTask.text = self.noteTextView.text
thisTask.imageURL = imageTitle
thisTask.thumbnail = thumbImageTitle
thisTask.id = unique
// Saving to CoreData
if context.save(nil) {
self.newAlert("Saving your note was successful!", title: "Notice")
self.noteTextView.text = ""
} else {
self.newAlert("Error saving your note, try again", title: "Notice")
}
} else {
self.newAlert("Error saving your image, try again", title: "Notice")
}
self.pickedImageView.image = UIImage(named: "P1000342.jpg")
}
我真的很感谢每一个建议。...如果您需要更多代码,请告诉我...
我注意到您与一起使用的质量因子大大降低了UIImageJPEGRepresentation
。如果试图减少内存占用,那么所有要做的就是减小NSData
写入持久性存储的结果的大小,但是将该图像加载到图像视图中仍然需要大约(4×宽度×高度)。 )字节(请注意,这是图片的尺寸,而不是图片视图)。因此,无论iPhone使用的质量因素如何,来自iPhone的3264×2448图像每个图像占用30mb UIImageJPEGRepresentation
。
通常,我会确保我的集合/表视图使用缩略图表示(自己的thumbnail
属性ALAsset
或自行调整默认表示的大小)。如果我要将该缩略图缓存到任何地方(例如您的问题所建议的持久性存储),则可以使用该缩略图的高质量表示形式(我使用PNG是因为它在压缩时是无损的,但是质量系数为0.7-0.9的JPEG是同样合理的版本)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句