WKImage always return nil - ios

Access assets image will always return nil, below is my code and assets catalog screen shot.
let image = WKImage(imageName: "sample")
print(image.image)
This will always prints nil.
Update: Updated screen shot

I found that from your screenshot you are setting the Image Set in Assests.xcassets in the swiftWatch WatchOS target and i think you are use that image for the WKDemo target so you get that nil.
Set that Image Set in your WKDemo's Assests.xcassets instead of WatchOS's Assests.xcassets then check.
That issue is your are setting ImageSet in different Target and your are try to load in Different Target.
UPDATE
After checking your sample project you are doing wrong code for getting image. Instead of let image = WKImage(imageName: "sample") you must be use WKPickerItem() object like following code:
for i in 1...10 {
let item = WKPickerItem()
item.title = "Picker itme =\(i)"
item.contentImage = WKImage(imageName: "sample")
if let image = item.contentImage
{
print(image)
}
pickerItems.append(item)
}
OUTPUT IS

Related

UIImage returns an empty image path from the gallery only in iOS 10.3.2

I have a problem in react-native, I'm creating a component in swift in order to do image processing. In my code, a picker selects the image from the gallery and returns the link of the image to me and then opens this image in my native code. My code works on two iPhones ( iPhone 6S on 12.3.1 and iPhone 7 on 12.3.1) but the link, once inserted in the UIImage object, returns me nil on iPhone 7 on 10.3.2 .
let result = val!["filename"] == nil ? "" : val!["filename"]
print(result)
self.bg = UIImage(named: result as! String )
if(bg == nil){
print("ERROR : Nil image")
}
So I do have the following messages that appears
/var/mobile/Media/DCIM/100APPLE/IMG_0628.JPG
ERROR : Nil image
Normally, there should be the image that generates well, but not in this version of iOS, is there something I missed? Another way to open the image in previous versions ?
You are using the wrong method to get image. Actually method that you have used is consider when image need to get from Bundle or Assets.
You need to use following method to fix your issue.
self.bg = UIImage(contentsOfFile: result as! String)

how to remove thumbnailData in CSSearchableItemAttributeSet

My indexed data don't have a photo, so I don't want to show a photo, I only want the title and description:
I have tried to set:
attributes.thumbnailData = nil
But it stil shows this blank image.
static func setupSearchableContentForSpotlight() {
let realm = try! Realm(configuration:Constants.realmConfigration.wordsConfigration)
var words: Results<Word>!
words = realm.objects(Word.self)
var searchableItems = [CSSearchableItem]()
words?.forEach { word in
let attributes = CSSearchableItemAttributeSet(itemContentType: kUTTypeData as String)
attributes.title = word.defination
attributes.contentDescription = word.meaning
attributes.thumbnailData = nil
let searchableVegetable = CSSearchableItem(uniqueIdentifier: nil, domainIdentifier: nil, attributeSet: attributes)
}
CSSearchableIndex.default().indexSearchableItems(searchableItems) { (error) -> Void in
print("indxing completed")
UserDefaults.standard.set(true, forKey: "spotLightIndexed")
if let error = error {
print(error.localizedDescription)
}
}
}
I expect to for example the result to be with only:
exams
الماس
without this blank image.
note: the app already has display icons.
An image is always shown next to a Core Spotlight result. If you don't supply a thumbnail image, then iOS will show your app icon.
Ensure that you have the right size icons in your icon set. You need a 40x40 icon at standard, 2x and 3x resolutions.
The problem solved by restarting my iPhone.
Extra tip: if you are dealing with CoreSpotlight, restaurant your iPhone after significant changes.

Image Array Causing memory Issue

Currently creating an basic image slideshow application, To do this i have created an UIImage array and calling them with the following code.
let imageNames = (0...50).map
{
"\($0).JPG"
}
let image = UIImage(named: imageNames[0])
imageView.animationImages = image
imageView.animationDuration = 50
imageView.startAnimating()
Was wondering if anyone would be able to offer some advice.
The issue isn't how much memory the image takes on disk, it's about the memory required to address all the pixels on your screen. You don't really need to store the images before you use them. One image of less than 1MB will load very quickly.
Instead you just need to keep a path to the image, and only when you get to the code that displays the image, then you load it, and display it.
So, combining Alexander's and KKRocks on-point answers:
In the top of your class, define your array of filenames:
let imageNames = (0...55).map { "\($0).JPG" }
Where you are displaying your image:
if let image = UIImage(named: images[0]) {
... the code that is blowing up ...
}
If you are not sure how to save a file and retrieve it, please see this answer I recently gave on that subject:
Saving and Retrieving Files in User's Space.
You need to add image name in array instead of UIImage .
let images: [String] = [
"0.JPG",
"1.JPG")!]
Get image from string of array
let image = UIImage(named: images[0])

UIImage returns nil in UITests

in my UITests I want compare image of element with the image from .xcassets:
let referenceImage = UIImage(named: "referenceImage")
guard referenceImage != nil else {
print("didn't find referenceImage image in assets")
XCTAssert(false)
return
}
let cellImage = myCell.images.elementBoundByIndex(0)
let imagesEqual = (referenceImage!.isEqual(cellImage))
XCTAssert(imagesEqual)
The referenceImage is always nil, although I've added Assets.xcassets to my UITests target (double checked in UITest target->Build phases/Copy bundle resources).
What can be wrong here?
UIImage uses the main bundle to look for the image.
The test bundle is a different bundle.
You should use UIImage's
init(named:in:compatibleWith:)
where you can specify your test bundle explicitly.
You can get the bundle for the test target with Bundle's initForClass , passing in your current test class.

Copy Image with UIPasteBoard (Swift)

I recently saw this project in which a user can tap on a GIF from a custom keyboard and they would see a "copied" toolip appear. I have one question:
How does one reproduce this tooltip in the products GIF-Tutorial?
Could anyone give me some sample code to work with. I understand how to use UIPasteboard and it's functions, but I can't seem to get it to work when I put in the UTI type "public.png" in this function: (I noticed in Objective-c it's "#public.png", but I placed "public.png" I couldn't find a source online for this)
let imageURL = NSString(string:NSBundle.mainBundle().pathForResource("test", ofType: "png")!)
var data = NSData(contentsOfURL: NSURL(string:imageURL)!)
UIPasteboard.generalPasteboard().setData(data!, forPasteboardType: "public.png")
Try using this code:
let image = UIImage(named: "myimage.png")
UIPasteboard.generalPasteboard().image = image;
you can find out how this works here!
Hope this helps
Swift 5.1
UIPasteboard.general.image = image
When using UIPasteboard.generalPasteboard().image = image; it seems the image is not copied to the pasteboard. Instead try the next code, it also explains how you can replace "public.png" string:
// The Pasteboard is nil if full access is not granted
// 'image' is the UIImage you about to copy to the pasteboard
if let pb = UIPasteboard.generalPasteboard() {
let type = UIPasteboardTypeListImage[0] as! String
if !type.isEmpty {
pb.setData(UIImagePNGRepresentation(image), forPasteboardType: type)
if let readData = pb.dataForPasteboardType(type) {
let readImage = UIImage(data: readData, scale: 2)
println("\(image) == \(pb.image) == \(readImage)")
}
}
}

Resources