ios – 使用大型UIImage数组时的内存问题导致崩溃(swift)

ios – 使用大型UIImage数组时的内存问题导致崩溃(swift),第1张

概述在我的应用程序中,我有一个图像阵列,可以保存我相机拍摄的所有图像.我正在使用collectionView来显示这些图像.但是,当此图像阵列到达大约20张图像时,它会崩溃.我相信这是由于内存问题..如何以一种内存效率的方式将图像存储在图像阵列中? Michael Dauterman使用缩略图提供了答案.我希望除此之外还有一个解决方案.也许将图片存储到NSData或CoreData中? Camera. 在我的应用程序中,我有一个图像阵列,可以保存我相机拍摄的所有图像.我正在使用collectionVIEw来显示这些图像.但是,当此图像阵列到达大约20张图像时,它会崩溃.我相信这是由于内存问题..如何以一种内存效率的方式将图像存储在图像阵列中?

Michael Dauterman使用缩略图提供了答案.我希望除此之外还有一个解决方案.也许将图片存储到NSData或CoreData中?

Camera.swift:

//What happens after the picture is chosenfunc imagePickerController(picker:UIImagePickerController,dIDFinishPickingMediawithInfo info: [NSObject:AnyObject]){    //cast image as a string    let mediaType = info[UIImagePickerControllerMediaType] as! Nsstring    self.dismissVIEwControllerAnimated(true,completion: nil)    //if the mediaType it actually is an image (jpeg)    if mediaType.isEqualToString(kUTTypeImage as Nsstring as String){        let image = info[UIImagePickerControllerOriginalimage] as! UIImage        //Our outlet for imagevIEw        appraisalPic.image = image        //Picture taken,to be added to imageArray        globalPic = image        //image:dIDFinish.. if we arent able to save,pass to contextInfo in Error Handling        if (newMedia == true){            UIImageWritetoSavedPhotosAlbum(image,self,"image:dIDFinishSavingWithError:contextInfo:",nil)        }    }}

NewRecord.swift

var imageArray:[UIImage] = [UIImage]()vIEwDIDLoad(){    //OUR IMAGE ARRAY WHICH HolDS OUR PHOTOS,CRASHES AROUND 20th PHOTO ADDED    imageArray.append(globalPic)//Rest of NewRecord.swift is code which adds images from imageArray to be presented on a collection vIEw}
解决方法 我自己的应用程序中遇到了低内存问题,这些问题必须与许多高分辨率UIImage对象一起使用.

解决方案是在imageArray中保存图像的缩略图(这会占用更少的内存),然后显示这些缩略图.如果用户确实需要查看全分辨率图像,您可以允许他们点击图像,然后重新加载&从相机胶卷显示全尺寸UIImage.

这里有一些代码可以让你创建缩略图:

// image here is your original imagelet size = CGSizeApplyAffinetransform(image.size,CGAffinetransformMakeScale(0.5,0.5))let hasAlpha = falselet scale: CGfloat = 0.0 // automatically use scale factor of main screenUIGraphicsBeginImageContextWithOptions(size,!hasAlpha,scale)image.drawInRect(CGRect(origin: CGPointZero,size: size))let scaledImage = UIGraphicsGetimageFromCurrentimageContext()UIGraphicsEndImageContext()imageArray.append(scaledImage)

和more information about these techniques can be found in this NSHipster article.

斯威夫特4 –

// image here is your original imagelet size = image.size.applying(CGAffinetransform(scaleX: 0.5,y: 0.5))let hasAlpha = falselet scale: CGfloat = 0.0 // automatically use scale factor of main screenUIGraphicsBeginImageContextWithOptions(size,scale)image.draw(in: CGRect(origin: .zero,size: size))let scaledImage = UIGraphicsGetimageFromCurrentimageContext()UIGraphicsEndImageContext()
总结

以上是内存溢出为你收集整理的ios – 使用大型UIImage数组时的内存问题导致崩溃(swift)全部内容,希望文章能够帮你解决ios – 使用大型UIImage数组时的内存问题导致崩溃(swift)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1101802.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存