Most efficient way to store an often changed array of array of images in core data
up vote
0
down vote
favorite
So I have been doing research on how to store an array of images and I am currently using an extension method suggested in another stack overflow post.
extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()
for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}
return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}
extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.compactMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}
So then when I save the array I simply use images.coreDataRepresentation() and store it in a binary data attribute of my entity.
My fear is that as the array may contain upwards of 900 images, and that constantly saving all of this data would result in delays. I was thinking that using a system similar to that of relational databases where I keep a separate core data entity to hold solely these photos with a relationship based on id, however, after doing more research I found that this is not advised with core data. Is there a better way to do this? Or is one of these methods superior?
swift core-data uiimage
|
show 1 more comment
up vote
0
down vote
favorite
So I have been doing research on how to store an array of images and I am currently using an extension method suggested in another stack overflow post.
extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()
for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}
return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}
extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.compactMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}
So then when I save the array I simply use images.coreDataRepresentation() and store it in a binary data attribute of my entity.
My fear is that as the array may contain upwards of 900 images, and that constantly saving all of this data would result in delays. I was thinking that using a system similar to that of relational databases where I keep a separate core data entity to hold solely these photos with a relationship based on id, however, after doing more research I found that this is not advised with core data. Is there a better way to do this? Or is one of these methods superior?
swift core-data uiimage
How big are the images? Have you tested with 900 images to see how it performs?
– Mike Taverne
Nov 12 at 17:42
@MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
– joshLor
Nov 12 at 17:53
@joshLor Wouldn’t it be better to store images insideDocuments Directoryand store a reference to them inside Core Data entity’s attribute
– Shubham Bakshi
Nov 12 at 18:17
@ShubhamBakshi What advantage does this give over just saving in core data
– joshLor
Nov 13 at 1:04
@joshLorCore Datais not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directoryare all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
– Shubham Bakshi
Nov 13 at 4:56
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So I have been doing research on how to store an array of images and I am currently using an extension method suggested in another stack overflow post.
extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()
for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}
return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}
extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.compactMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}
So then when I save the array I simply use images.coreDataRepresentation() and store it in a binary data attribute of my entity.
My fear is that as the array may contain upwards of 900 images, and that constantly saving all of this data would result in delays. I was thinking that using a system similar to that of relational databases where I keep a separate core data entity to hold solely these photos with a relationship based on id, however, after doing more research I found that this is not advised with core data. Is there a better way to do this? Or is one of these methods superior?
swift core-data uiimage
So I have been doing research on how to store an array of images and I am currently using an extension method suggested in another stack overflow post.
extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()
for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}
return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}
extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.compactMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}
So then when I save the array I simply use images.coreDataRepresentation() and store it in a binary data attribute of my entity.
My fear is that as the array may contain upwards of 900 images, and that constantly saving all of this data would result in delays. I was thinking that using a system similar to that of relational databases where I keep a separate core data entity to hold solely these photos with a relationship based on id, however, after doing more research I found that this is not advised with core data. Is there a better way to do this? Or is one of these methods superior?
swift core-data uiimage
swift core-data uiimage
asked Nov 12 at 17:31
joshLor
477316
477316
How big are the images? Have you tested with 900 images to see how it performs?
– Mike Taverne
Nov 12 at 17:42
@MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
– joshLor
Nov 12 at 17:53
@joshLor Wouldn’t it be better to store images insideDocuments Directoryand store a reference to them inside Core Data entity’s attribute
– Shubham Bakshi
Nov 12 at 18:17
@ShubhamBakshi What advantage does this give over just saving in core data
– joshLor
Nov 13 at 1:04
@joshLorCore Datais not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directoryare all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
– Shubham Bakshi
Nov 13 at 4:56
|
show 1 more comment
How big are the images? Have you tested with 900 images to see how it performs?
– Mike Taverne
Nov 12 at 17:42
@MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
– joshLor
Nov 12 at 17:53
@joshLor Wouldn’t it be better to store images insideDocuments Directoryand store a reference to them inside Core Data entity’s attribute
– Shubham Bakshi
Nov 12 at 18:17
@ShubhamBakshi What advantage does this give over just saving in core data
– joshLor
Nov 13 at 1:04
@joshLorCore Datais not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directoryare all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
– Shubham Bakshi
Nov 13 at 4:56
How big are the images? Have you tested with 900 images to see how it performs?
– Mike Taverne
Nov 12 at 17:42
How big are the images? Have you tested with 900 images to see how it performs?
– Mike Taverne
Nov 12 at 17:42
@MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
– joshLor
Nov 12 at 17:53
@MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
– joshLor
Nov 12 at 17:53
@joshLor Wouldn’t it be better to store images inside
Documents Directory and store a reference to them inside Core Data entity’s attribute– Shubham Bakshi
Nov 12 at 18:17
@joshLor Wouldn’t it be better to store images inside
Documents Directory and store a reference to them inside Core Data entity’s attribute– Shubham Bakshi
Nov 12 at 18:17
@ShubhamBakshi What advantage does this give over just saving in core data
– joshLor
Nov 13 at 1:04
@ShubhamBakshi What advantage does this give over just saving in core data
– joshLor
Nov 13 at 1:04
@joshLor
Core Data is not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directory are all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet– Shubham Bakshi
Nov 13 at 4:56
@joshLor
Core Data is not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directory are all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet– Shubham Bakshi
Nov 13 at 4:56
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267267%2fmost-efficient-way-to-store-an-often-changed-array-of-array-of-images-in-core-da%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
How big are the images? Have you tested with 900 images to see how it performs?
– Mike Taverne
Nov 12 at 17:42
@MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
– joshLor
Nov 12 at 17:53
@joshLor Wouldn’t it be better to store images inside
Documents Directoryand store a reference to them inside Core Data entity’s attribute– Shubham Bakshi
Nov 12 at 18:17
@ShubhamBakshi What advantage does this give over just saving in core data
– joshLor
Nov 13 at 1:04
@joshLor
Core Datais not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directoryare all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet– Shubham Bakshi
Nov 13 at 4:56