Show gallery picture in app swift NSRangeException error





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I find tutorial who make gallery in app with swift I want to show picture in m'y application but it dosen't work.



there is my CollectionController:



import UIKit
import Photos

class CollectionController: UICollectionViewController {

var imageArray = [UIImage]()

override func viewDidLoad() {
grabPhotos()
}

func grabPhotos(){

let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true

requestOptions.deliveryMode = .opportunistic

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)

if fetchResult.count > 0 {

for i in 0...fetchResult.count{

imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
image, error in
self.imageArray.append(image!)
}
}
}
else{
print("You don't have photos");
self.collectionView.reloadData()
}
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

let imageView = cell.viewWithTag(1) as! UIImageView

imageView.image = imageArray[indexPath.row]

return cell

}

}


myCell.swift :



import UIKit

class myCell: UICollectionViewCell {

@IBOutlet weak var myImageView: UIImageView!
}


this code return the following error and I have white screen and Thread 1: signal SIGABRT on the AppDelegate



2018-11-23 17:14:56.499653+0100 My app[1584:453752] *** Terminating app due to uncaught exception 'NSRangeException', reason: '0x28276c900: index (551) beyond bounds (551)'
*** First throw call stack:
(0x212243ef8 0x211411a40 0x212148ac4 0x220fcd434 0x220fee08c 0x1001db530 0x1001db0f4
0x1001db128 0x23f189e4c 0x23f18a27c 0x23f261e90 0x23f262438 0x23f27342c 0x23eabfe10
0x23eac57ac 0x23f2ecdb8 0x23f2e9364 0x23f2eca34 0x23f2ed3d4 0x23f2ac5fc 0x23f2ac2a8
0x23f2ef844 0x23f2f0334 0x23f2ef6fc 0x23f2e8a10 0x23eac3ca4 0x23eaf545c 0x214c73890
0x214c7e658 0x214c7dd50 0x1011c0de4 0x1011c4a2c 0x214cb2640 0x214cb22cc 0x214cb28e8
0x2121d25b8 0x2121d2538 0x2121d1e1c 0x2121ccce8 0x2121cc5b8 0x214440584 0x23eac7558
0x1001d62e8 0x211c8cb94)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)


I don't understand why !



Please help me fix the issues.



thanks!!










share|improve this question































    0















    I find tutorial who make gallery in app with swift I want to show picture in m'y application but it dosen't work.



    there is my CollectionController:



    import UIKit
    import Photos

    class CollectionController: UICollectionViewController {

    var imageArray = [UIImage]()

    override func viewDidLoad() {
    grabPhotos()
    }

    func grabPhotos(){

    let imgManager = PHImageManager.default()
    let requestOptions = PHImageRequestOptions()
    requestOptions.isSynchronous = true

    requestOptions.deliveryMode = .opportunistic

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

    let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)

    if fetchResult.count > 0 {

    for i in 0...fetchResult.count{

    imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
    image, error in
    self.imageArray.append(image!)
    }
    }
    }
    else{
    print("You don't have photos");
    self.collectionView.reloadData()
    }
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageArray.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

    let imageView = cell.viewWithTag(1) as! UIImageView

    imageView.image = imageArray[indexPath.row]

    return cell

    }

    }


    myCell.swift :



    import UIKit

    class myCell: UICollectionViewCell {

    @IBOutlet weak var myImageView: UIImageView!
    }


    this code return the following error and I have white screen and Thread 1: signal SIGABRT on the AppDelegate



    2018-11-23 17:14:56.499653+0100 My app[1584:453752] *** Terminating app due to uncaught exception 'NSRangeException', reason: '0x28276c900: index (551) beyond bounds (551)'
    *** First throw call stack:
    (0x212243ef8 0x211411a40 0x212148ac4 0x220fcd434 0x220fee08c 0x1001db530 0x1001db0f4
    0x1001db128 0x23f189e4c 0x23f18a27c 0x23f261e90 0x23f262438 0x23f27342c 0x23eabfe10
    0x23eac57ac 0x23f2ecdb8 0x23f2e9364 0x23f2eca34 0x23f2ed3d4 0x23f2ac5fc 0x23f2ac2a8
    0x23f2ef844 0x23f2f0334 0x23f2ef6fc 0x23f2e8a10 0x23eac3ca4 0x23eaf545c 0x214c73890
    0x214c7e658 0x214c7dd50 0x1011c0de4 0x1011c4a2c 0x214cb2640 0x214cb22cc 0x214cb28e8
    0x2121d25b8 0x2121d2538 0x2121d1e1c 0x2121ccce8 0x2121cc5b8 0x214440584 0x23eac7558
    0x1001d62e8 0x211c8cb94)
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb)


    I don't understand why !



    Please help me fix the issues.



    thanks!!










    share|improve this question



























      0












      0








      0








      I find tutorial who make gallery in app with swift I want to show picture in m'y application but it dosen't work.



      there is my CollectionController:



      import UIKit
      import Photos

      class CollectionController: UICollectionViewController {

      var imageArray = [UIImage]()

      override func viewDidLoad() {
      grabPhotos()
      }

      func grabPhotos(){

      let imgManager = PHImageManager.default()
      let requestOptions = PHImageRequestOptions()
      requestOptions.isSynchronous = true

      requestOptions.deliveryMode = .opportunistic

      let fetchOptions = PHFetchOptions()
      fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

      let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)

      if fetchResult.count > 0 {

      for i in 0...fetchResult.count{

      imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
      image, error in
      self.imageArray.append(image!)
      }
      }
      }
      else{
      print("You don't have photos");
      self.collectionView.reloadData()
      }
      }

      override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return imageArray.count
      }

      override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

      let imageView = cell.viewWithTag(1) as! UIImageView

      imageView.image = imageArray[indexPath.row]

      return cell

      }

      }


      myCell.swift :



      import UIKit

      class myCell: UICollectionViewCell {

      @IBOutlet weak var myImageView: UIImageView!
      }


      this code return the following error and I have white screen and Thread 1: signal SIGABRT on the AppDelegate



      2018-11-23 17:14:56.499653+0100 My app[1584:453752] *** Terminating app due to uncaught exception 'NSRangeException', reason: '0x28276c900: index (551) beyond bounds (551)'
      *** First throw call stack:
      (0x212243ef8 0x211411a40 0x212148ac4 0x220fcd434 0x220fee08c 0x1001db530 0x1001db0f4
      0x1001db128 0x23f189e4c 0x23f18a27c 0x23f261e90 0x23f262438 0x23f27342c 0x23eabfe10
      0x23eac57ac 0x23f2ecdb8 0x23f2e9364 0x23f2eca34 0x23f2ed3d4 0x23f2ac5fc 0x23f2ac2a8
      0x23f2ef844 0x23f2f0334 0x23f2ef6fc 0x23f2e8a10 0x23eac3ca4 0x23eaf545c 0x214c73890
      0x214c7e658 0x214c7dd50 0x1011c0de4 0x1011c4a2c 0x214cb2640 0x214cb22cc 0x214cb28e8
      0x2121d25b8 0x2121d2538 0x2121d1e1c 0x2121ccce8 0x2121cc5b8 0x214440584 0x23eac7558
      0x1001d62e8 0x211c8cb94)
      libc++abi.dylib: terminating with uncaught exception of type NSException
      (lldb)


      I don't understand why !



      Please help me fix the issues.



      thanks!!










      share|improve this question
















      I find tutorial who make gallery in app with swift I want to show picture in m'y application but it dosen't work.



      there is my CollectionController:



      import UIKit
      import Photos

      class CollectionController: UICollectionViewController {

      var imageArray = [UIImage]()

      override func viewDidLoad() {
      grabPhotos()
      }

      func grabPhotos(){

      let imgManager = PHImageManager.default()
      let requestOptions = PHImageRequestOptions()
      requestOptions.isSynchronous = true

      requestOptions.deliveryMode = .opportunistic

      let fetchOptions = PHFetchOptions()
      fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

      let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)

      if fetchResult.count > 0 {

      for i in 0...fetchResult.count{

      imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
      image, error in
      self.imageArray.append(image!)
      }
      }
      }
      else{
      print("You don't have photos");
      self.collectionView.reloadData()
      }
      }

      override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return imageArray.count
      }

      override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

      let imageView = cell.viewWithTag(1) as! UIImageView

      imageView.image = imageArray[indexPath.row]

      return cell

      }

      }


      myCell.swift :



      import UIKit

      class myCell: UICollectionViewCell {

      @IBOutlet weak var myImageView: UIImageView!
      }


      this code return the following error and I have white screen and Thread 1: signal SIGABRT on the AppDelegate



      2018-11-23 17:14:56.499653+0100 My app[1584:453752] *** Terminating app due to uncaught exception 'NSRangeException', reason: '0x28276c900: index (551) beyond bounds (551)'
      *** First throw call stack:
      (0x212243ef8 0x211411a40 0x212148ac4 0x220fcd434 0x220fee08c 0x1001db530 0x1001db0f4
      0x1001db128 0x23f189e4c 0x23f18a27c 0x23f261e90 0x23f262438 0x23f27342c 0x23eabfe10
      0x23eac57ac 0x23f2ecdb8 0x23f2e9364 0x23f2eca34 0x23f2ed3d4 0x23f2ac5fc 0x23f2ac2a8
      0x23f2ef844 0x23f2f0334 0x23f2ef6fc 0x23f2e8a10 0x23eac3ca4 0x23eaf545c 0x214c73890
      0x214c7e658 0x214c7dd50 0x1011c0de4 0x1011c4a2c 0x214cb2640 0x214cb22cc 0x214cb28e8
      0x2121d25b8 0x2121d2538 0x2121d1e1c 0x2121ccce8 0x2121cc5b8 0x214440584 0x23eac7558
      0x1001d62e8 0x211c8cb94)
      libc++abi.dylib: terminating with uncaught exception of type NSException
      (lldb)


      I don't understand why !



      Please help me fix the issues.



      thanks!!







      ios swift






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 10:55







      rem

















      asked Nov 23 '18 at 16:33









      remrem

      649




      649
























          1 Answer
          1






          active

          oldest

          votes


















          0














          For the first compiler error, it is saying that you are using the if let condition on the item returning non-optional value.



          let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions){

          if fetchResult.count > 0 {

          for i in 0...fetchResult.count{

          imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
          image, error in
          self.imageArray.append(image!)

          }

          }

          }
          else{
          print("You don't have photos");
          self.collectionView.reloadData()
          }


          Update your code and share the results.






          share|improve this answer
























          • thanks for your answer i'ts work but now I have white screen and always the second error

            – rem
            Nov 26 '18 at 10:10











          • ok, update your question and provide more code from collectionView dataSource methods.

            – Bhavin Kansagara
            Nov 26 '18 at 10:14











          • it's m'y code !

            – rem
            Nov 26 '18 at 10:57












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450214%2fshow-gallery-picture-in-app-swift-nsrangeexception-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          For the first compiler error, it is saying that you are using the if let condition on the item returning non-optional value.



          let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions){

          if fetchResult.count > 0 {

          for i in 0...fetchResult.count{

          imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
          image, error in
          self.imageArray.append(image!)

          }

          }

          }
          else{
          print("You don't have photos");
          self.collectionView.reloadData()
          }


          Update your code and share the results.






          share|improve this answer
























          • thanks for your answer i'ts work but now I have white screen and always the second error

            – rem
            Nov 26 '18 at 10:10











          • ok, update your question and provide more code from collectionView dataSource methods.

            – Bhavin Kansagara
            Nov 26 '18 at 10:14











          • it's m'y code !

            – rem
            Nov 26 '18 at 10:57
















          0














          For the first compiler error, it is saying that you are using the if let condition on the item returning non-optional value.



          let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions){

          if fetchResult.count > 0 {

          for i in 0...fetchResult.count{

          imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
          image, error in
          self.imageArray.append(image!)

          }

          }

          }
          else{
          print("You don't have photos");
          self.collectionView.reloadData()
          }


          Update your code and share the results.






          share|improve this answer
























          • thanks for your answer i'ts work but now I have white screen and always the second error

            – rem
            Nov 26 '18 at 10:10











          • ok, update your question and provide more code from collectionView dataSource methods.

            – Bhavin Kansagara
            Nov 26 '18 at 10:14











          • it's m'y code !

            – rem
            Nov 26 '18 at 10:57














          0












          0








          0







          For the first compiler error, it is saying that you are using the if let condition on the item returning non-optional value.



          let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions){

          if fetchResult.count > 0 {

          for i in 0...fetchResult.count{

          imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
          image, error in
          self.imageArray.append(image!)

          }

          }

          }
          else{
          print("You don't have photos");
          self.collectionView.reloadData()
          }


          Update your code and share the results.






          share|improve this answer













          For the first compiler error, it is saying that you are using the if let condition on the item returning non-optional value.



          let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions){

          if fetchResult.count > 0 {

          for i in 0...fetchResult.count{

          imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
          image, error in
          self.imageArray.append(image!)

          }

          }

          }
          else{
          print("You don't have photos");
          self.collectionView.reloadData()
          }


          Update your code and share the results.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 17:36









          Bhavin KansagaraBhavin Kansagara

          2,06011018




          2,06011018













          • thanks for your answer i'ts work but now I have white screen and always the second error

            – rem
            Nov 26 '18 at 10:10











          • ok, update your question and provide more code from collectionView dataSource methods.

            – Bhavin Kansagara
            Nov 26 '18 at 10:14











          • it's m'y code !

            – rem
            Nov 26 '18 at 10:57



















          • thanks for your answer i'ts work but now I have white screen and always the second error

            – rem
            Nov 26 '18 at 10:10











          • ok, update your question and provide more code from collectionView dataSource methods.

            – Bhavin Kansagara
            Nov 26 '18 at 10:14











          • it's m'y code !

            – rem
            Nov 26 '18 at 10:57

















          thanks for your answer i'ts work but now I have white screen and always the second error

          – rem
          Nov 26 '18 at 10:10





          thanks for your answer i'ts work but now I have white screen and always the second error

          – rem
          Nov 26 '18 at 10:10













          ok, update your question and provide more code from collectionView dataSource methods.

          – Bhavin Kansagara
          Nov 26 '18 at 10:14





          ok, update your question and provide more code from collectionView dataSource methods.

          – Bhavin Kansagara
          Nov 26 '18 at 10:14













          it's m'y code !

          – rem
          Nov 26 '18 at 10:57





          it's m'y code !

          – rem
          Nov 26 '18 at 10:57




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450214%2fshow-gallery-picture-in-app-swift-nsrangeexception-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?