我正在制作一个iOS应用程序,显示用户的婚礼图像。图像文件的平均大小约为1.5 Mb。但当图像超过(超过10个图像)应用程序崩溃。有没有解决这个问题的方案? 我正在考虑实施核心数据。会有帮助吗?如果没有,请帮助我。
这是我的代码
func webservice() { let url = NSString(格式:" http://webphotobooks.in/webphotobook/index.php/web_controller/fetch1?id=%@&album_id=%@",self.userId,self.albumId)
let urlString :String = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
//print(urlString, terminator: "")
let request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: urlString as String)
request.HTTPMethod = "GET"
var response: NSURLResponse?
let data = (try? NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)) as NSData?
if let httpResponse = response as? NSHTTPURLResponse {
//print("error \(httpResponse.statusCode)")
let statusCode = httpResponse.statusCode
if statusCode == 200
{
do
{
let jsonResult: NSDictionary! = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
//print(jsonResult, terminator: "")
if (jsonResult != nil)
{
if (jsonResult?.objectForKey("messagge") as! String == "successfull")
{
self.array = jsonResult.valueForKey("images") as! NSArray
// print(self.array)
self.collectionView.reloadData()
}
else
{
if #available(iOS 8.0, *) {
let alert = UIAlertController(title: "Error", message: "Unable to fetch images", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.view.tintColor = UIColor.blackColor()
self.presentViewController(alert, animated: true, completion: nil)
}
else
{
let alert = UIAlertView(title: "Error", message: "Unable to fetch images", delegate: self, cancelButtonTitle: "Ok")
alert.show()
}
}
}
}
catch let error as NSError
{
print(error)
}
}
else
{
if #available(iOS 8.0, *) {
let alert = UIAlertController(title: "Error", message: "Please check your internet connection", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.view.tintColor = UIColor.blackColor()
self.presentViewController(alert, animated: true, completion: nil)
}
else
{
let alert = UIAlertView(title: "Error", message: "Please check your internet connection", delegate: self, cancelButtonTitle: "Ok")
alert.show()
}
}
}
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return self.array.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell:HomeCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("HomeCell", forIndexPath: indexPath) as! HomeCollectionViewCell
if let images = cell.profileImage
{
if !(array.valueForKey("image_name").objectAtIndex(indexPath.row).isKindOfClass(NSNull))
{
let Str = array.valueForKey("image_name").objectAtIndex(indexPath.row) as! String
let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: NSURL!) -> Void in
// print(image)
}
let urlStr = NSString(format: "http://webphotobooks.in/admin/uploads/category_pics/%@", Str)
//print(urlStr)
let urlString :String = urlStr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
//print(urlString)
let url = NSURL(string: urlString as String)
//print(url)
images.sd_setImageWithURL(url, completed: block)
}
}
actInd.stopAnimating()
return cell
}
答案 0 :(得分:1)
图像的平均大小约为1.5 Mb
这是文件大小而不是图像大小,如果它是jpg也可能是压缩的。在内存中打开后,图像将num_pixel_height * num_pixel_width * num_channel * bit_for_channel
。例如,用于通道200px * 200px的8位图像RGBA约为625kb,但由于压缩而导致的文件大小可能要小得多。
答案 1 :(得分:1)
由于你消耗了太多的内存,肯定会崩溃。唯一的解决方案是获取小尺寸图像或在获取图像后压缩图像。您无法将所有完整大小的图像加载到内存中。后端有许多压缩图像的选项。您可以在后端本身压缩图像,这可能会减少您在应用中的工作。
您可能还想查看实际图片。然后,您可以在点击图像后下载实际图像。最初,您应该获取较小的图像。每个应用程序都这样做。有许多开源图像缓存库甚至可以为此目的处理缓存。一个这样的例子是SD-WebImage