我正在製作一個顯示用戶婚禮圖像的iOS應用程序。圖像文件的平均大小約爲1.5 Mb。但是,當圖像超過(超過10個圖像)應用程序崩潰。有沒有解決這個問題的方法? 我正在考慮實施核心數據。它會有幫助嗎?如果不是,請幫助我。加載大尺寸圖片使Swift 2.0中的應用程序崩潰
這是我的代碼
FUNC web服務() { 設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
}
顯示您的代碼以及它如何崩潰。嘗試使用'UIImage(contentsOfFile:)'initializer加載它們 –
我正在使用SDWebImages來加載圖像 –