2

I have an array called imageCollection which contains all the photos on the device. I want to display these in a more robust way, for example as a photos collage on screen. In order to do this I have created a struct ImageObject which has additional properties like the x and y coordinates. I want to store a group of ImageObjects in an array imageObjectGroup, where each instance contains one image from my camera roll. Ultimately creating an instance of imageObject for each photo in my camera roll. I need help constructing this for loop that creates a new imageObjects instance. It can be found at the bottom of the function that populates imageCollection (array with all images from camera roll)

Code:

// Array containing all the images from camera roll
var imageCollection = [PHAsset]()

// Populating array with all images for loop is also here that adds each photo as a imageObjects instance
func getImages() {
    let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
    assets.enumerateObjects({ (object, count, stop) in
       // self.cameraAssets.add(object)
        imageCollection.append(object)
    })

    //In order to get latest image first, we just reverse the array
    imageCollection.reverse()
    
    // ADDING INSTANCES OF ‘imageObjectGroup’ HERE 
    for index in imageCollection{
        print(index);
    }
}

// The images array is where each photo instance will be stored
struct ImageObject: Identifiable {
    let id = UUID()
    let image = PHAsset()
    /// Card x position
    var x: CGFloat = 0.0
    /// Card y position
    var y: CGFloat = 0.0
    /// Card rotation angle
    var degree: Double = 0.0
}

var imageObjectGroup = [ImageObject]()
4
  • let images = [PHAsset]() shouldn't this be let image = PHAsset()? Commented Aug 7, 2021 at 20:03
  • You're right @aheze it should only be one instance of a photo, thank you I've edited the code to reflect this Commented Aug 7, 2021 at 20:07
  • Thanks. But where are you going to get the x, y, and degree values? If you can edit your code to show that, I can post an answer. Commented Aug 7, 2021 at 20:09
  • @aheze All of the coordinates / degree are defaulting to 0 during initialization. I have them because I plan to use them in case I spawn multiple photos and have them move around on screen but I plan to handle any changes in their values after initialization Commented Aug 7, 2021 at 20:23

1 Answer 1

1

Why not just do:

for phAsset in imageCollection {
    let newImageObject = ImageObject(image: phAsset) /// make a new `ImageObject` 
    imageObjectGroup.append(newImageObject) /// append it
}

or this one-liner:

imageObjectGroup = imageCollection.map { ImageObject(image: $0) }

...Just make sure to change let image = PHAsset() to var image = PHAsset(), otherwise you won't be able to change the image.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, this worked perfectly. Not sure why this part confused me so much your answer makes it a lot simpler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.