How can i show Images from Sqlite to a uicollectionview - swift

I have some images in sqlite database. I saved Image in BLOB type (I converted in img.pngdata as NSData and save it to db). Now I want to retrieve images and show them in a uicollectionview view. when I retrieve image it is like this
1. Which type of array should I use to show them in collection view? The table in the db is like this2.
Also, how can I get the image name as a string from the retrieved image?

I solved the issue by retrieving the blob data like this:
let lenght:Int = Int(sqlite3_column_bytes(queryStatement, 0));
let imgdata : NSData = NSData(bytes: sqlite3_column_blob(queryStatement, 0), length: lenght)
let decoded_img : UIImage = UIImage(data: imgdata as Data)!
imageNames.append(decoded_img)
I declared an array of type UIImage, then used it in uicollectionview view, it is now working fine for me.

Related

Converting UIImage to Data throws unsupported file format

I'm using the image picker to select a photo from photo library or to take a camera and in my imagePickerController function, I get the UIImage and try to convert it to an Image Data like this
let uiImage = info.[UIImagePickerController.Infokey.originalImage] as! UIImage
let imageData = uiImage.jpegData(compressionQuality: 1)! // this line throws error
And I keep getting this error
findWriterForTypeAndAlternateType:119: unsupported file format 'public.heic'
And also when I try to update my imageview like this Image(uiImage) the view gets updated but I don't know why I'm getting that error.
Can someone tell me what I'm doing wrong?
Okay... I've got it working. I don't know for what reason, but I changed myimageData to this:
let imageData = Data((uiImage.pngData())!)

create image from data cocoa swift 5

I have a set of Data coming from a request (AlamoFire) as per the image below:
I know it's an image, the size of the data is correct. I'd like to create an image from this and tried:
NSImage(data: result) or
NSImage.init(data:result)
Without success. It is possible to do what I try to achieve?
EDIT: to answer Larme on Self.coverImage:
did you try to use this code snippet?
// Swift
// Image to data
let image = UIImage(named: "sample")
let data = image?.pngData()
let data = image?.jpegData(compressionQuality: 0.9)
// Data to Image
let uiImage: UIImage = UIImage(data: imageData)
// this method is deprecated
var imageData: Data = UIImagePNGRepresentation(image)

How to get and save GIF Image from NSImageView to disk

Currently I am working on an NSImageView, on which the user drags and drops an image, and the images gets saved to the disk. I am able to save png and jpeg images, but while saving GIF images, all that is saved is a single frame from the gif. The imageView is able to display the whole animated gif though.
My current implementation to save image from NSImageView to disk is:
let cgRef = image.CGImageForProposedRect(nil, context: nil, hints: nil)
let newRep = NSBitmapImageRep(CGImage: cgRef!)
newRep.size = image.size
let type = getBitmapImageFileType(imageName.lowercaseString) // getBitmapImageFileType: returns NSBitmapImageFileType
let properties = type == .NSGIFFileType ? [NSImageLoopCount: 0] : Dictionary<String, AnyObject>()
let data: NSData = newRep.representationUsingType(type, properties: properties)!
data.writeToFile(link, atomically: true)
How should I modify this code to be able to save all the frames of GIF to the disk.

Saving image into core data and fetching it into tableview cell

I did a set up of CoreData with entity "users" and attribute "username" (when I type something in uitextfield it saves into core data and I got readings from core data into tableview cell). That is working like a charm. But I also want to display chosen picture from imagepickercontroller and that is also working fine when I choose picture but when I click save I think I got it saved. I don't know if it is saved because I eliminated all error so I got no errors, but it won't read that picture into tableview cell. I put new attribute "image" with type binary data into core data file but code does not work.
This is code for saving into core data
let newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context)
newUser.setValue(nameField.text, forKey: "username")
let picture = UIImageJPEGRepresentation(photoImageView.image!, 1);
newUser.setValue(picture, forKey: "image")
And this is code where tableview reads data from core data
cell.imageView!.image = person.valueForKey("image") as? UIImage
I also tried with
[UIImage, imageWithData:self.myEvent.picture];
but Swift 2.0 don't have that syntax imageWithData
person.valueForKey("image") as? UIImage won't work.
Use UIImage(data:) to convert the retrieved NSData to UIImage.
for retrieving image from core data i used this and it is finally working.
let image = person.valueForKey("image") as? NSData
cell.imageView!.image = UIImage(data: image!)

UIImagePNGRepresentation(UIImage()) returns nil

Why does UIImagePNGRepresentation(UIImage()) returns nil?
I'm trying to create a UIImage() in my test code just to assert that it was correctly passed around.
My comparison method for two UIImage's uses the UIImagePNGRepresentation(), but for some reason, it is returning nil.
Thank you.
UIImagePNGRepresentation() will return nil if the UIImage provided does not contain any data. From the UIKit Documentation:
Return Value
A data object containing the PNG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
When you initialize a UIImage by simply using UIImage(), it creates a UIImage with no data. Although the image isn't nil, it still has no data. And, because the image has no data, UIImagePNGRepresentation() just returns nil.
To fix this, you would have to use UIImage with data. For example:
var imageName: String = "MyImageName.png"
var image = UIImage(named: imageName)
var rep = UIImagePNGRepresentation(image)
Where imageName is the name of your image, included in your application.
In order to use UIImagePNGRepresentation(image), image must not be nil, and it must also have data.
If you want to check if they have any data, you could use:
if(image == nil || image == UIImage()){
//image is nil, or has no data
}
else{
//image has data
}
The UIImage documentation says
Image objects are immutable, so you cannot change their properties after creation. This means that you generally specify an image’s properties at initialization time or rely on the image’s metadata to provide the property value.
Since you've created the UIImage without providing any image data, the object you've created has no meaning as an image. UIKit and Core Graphics don't appear to allow 0x0 images.
The simplest fix is to create a 1x1 image instead:
UIGraphicsBeginImageContext(CGSizeMake(1, 1))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
I faced the same issue i was converting UIImage to pngData but sometime it returns nil. i fixed it by just creating copy of image
func getImagePngData(img : UIImage) -> Data {
let pngData = Data()
if let hasData = img.pngData(){
print(hasData)
pngData = hasData
}
else{
UIGraphicsBeginImageContext(img.size)
img.draw(in: CGRect(x: 0.0, y: 0.0, width: img.width,
height: img.height))
let resultImage =
UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
print(resultImage.pngData)
pngData = resultImage.pngData
}
return pngData
}