I am working with SVGKit in swift Project. I properly imported SVGKit Framework and 3rd party resources folder into my project by following (https://github.com/SVGKit/SVGKit).
I created a folder with sets of .svg images inside the project and i am using the following code to load image into testIcon(ImageView).
let lockImage: SVGKImage = SVGKImage(named: "LockIcon.svg")
let lockImageLocal: UIImage = lockImage.uiImage
testIcon.image = lockImageLocal
But i am receiving the following error,
** Assertion failure in +[SVGKImage imageWithSource:], /Users/fss/Downloads/SVGKit-
2.x/Source/SVGKImage.m:229
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not
satisfying: newSource != nil'
Please let me know how to mention path of .svg image(which is inside my project) to SVGKImage image class or how to resolve this problem.
please remove .svg from image name. This method named: doesn't need extension.
Or you can try this way too...
Swift 4
if let imagePath = Bundle.main.path(forResource: "imageName", ofType: "svg")
{
let lockImage: SVGKImage = SVGKImage(contentsOfFile: imagePath)
let lockImageLocal: UIImage = lockImage.uiImage
testIcon.image = lockImageLocal
}
Obj-c type
NSString *imagePath = [NSBundle mainBundle][ pathForResource:#"imageName" ofType:#"svg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
Related
App keeps crashing on this Method - trying to simply pin a Parse object to the local data store as outlined in the docs Parse docs:
func saveToBackground(title:String, description:String, coords:CLLocationCoordinate2D, image:UIImage, objectId:String, dateFound:String){
let imageData = image.jpeg(.low)
let imageFile = PFFile(name: "image.png", data: imageData!)
let foundObject = PFObject(className: "FoundObjects")
foundObject["title"] = title
foundObject["description"] = description
foundObject["coordinates"] = coords
foundObject["image"] = imageFile
foundObject["objectId"] = objectId
foundObject["dateFound"] = dateFound
foundObject.pinInBackground()
}
error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'PFObject values may not have class: NSConcreteValue'
Any ideas anyone?
At least one of your values is an NSConcreteValue class, which can't be stored on a PFObject. Figure out which line is causing the issue by setting a breakpoint and stepping through, and make sure to cast that value to the expected class. Or you could cast all of these to their expected class, cover your bases.
Edit: As pointed out by MartynE23, the issue was actually the CLLocationCoordinate2D, which must be converted to a PFGeoPoint to add to a Parse Object
How do I create an MSSticker from a UIImage? The only initializer for an MSSticker is from a local URL, which makes me believe that I first must write the UIImage to a file. If that's the case, could someone explain how to do this because I have had trouble doing so. I was successfully able to write to a file using NSData.writeToFile(), but I could not figure out what URL to pass to the MSSticker initializer.
There's a solution readily available in this other thread: iOS 10 Message Extension app: How can I get the MSSticker created by this function?
The idea is the same you mention in your question: save the images to disk, and then load the MSSticker instance from them.
You can create MSSticker using UIImage , first you need to put resources in bundle not is assets.xcassets folder. after that just use this simple method to create sticker object.
let imagePath = Bundle.main.path(forResource: imgName, ofType: ".png")
let pathurl = URL(fileURLWithPath: imagePath!)
do {
try cell.stickerview.sticker = MSSticker(contentsOfFileURL: pathurl, localizedDescription: "anything that you want")
}
catch {
fatalError("Failed to create sticker: \(error)")
}
well am trying this :
if let storedCoverImagePath = UC.coverImagePath{
let storedCoverImage = UIImage(contentsOfFile: storedCoverImagePath as String)
self.coverImage.image = storedCoverImage
self.backgroundImageView.image = storedCoverImage
}
and the value of storedCoverImagePath is :
/Users/remy/Library/Developer/CoreSimulator/Devices/D29D4F6F-E146-419D-B4B8-B1914F56569F/data/Containers/Data/Application/637675BB-EFA1-A8RT-8B73-A3628/DocumentsCoverImage.jpg
its straight forward that am trying to get an image from this storedCoverImagePath path. i double checked the path, image is there still am not able to set the image using this path, is there is something that am missing if yes than tell me please
You have to specify path relative to your apps bundle or directory. If you hardcode the path like you did:
/Users/remy/Library/Developer/CoreSimulator/Devices/D29D4F6F-E146-419D-B4B8-B1914F56569F/data/Containers/Data/Application/637675BB-EFA1-A8RT-8B73-A3628/DocumentsCoverImage.jpg
it gets ignored because of the sandboxing. iOS does not allow the app to reference directories external to the sandbox.
Possible solutions
One way to get it working is to add the image to the project. Then you can get it using:
let image : UIImage = UIImage(named:"ImageName")
Another way, if the image is saved in documents directory of your app, is to use:
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent(yourImageName)
let image = UIImage(contentsOfFile: fileURL.path!)
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)")
}
}
}
I'm developing an iOS 5.0+ app with latest SDK.
On my app I have some C++ image recognition software, and I'm trying to save image recognized by this C++ functions.
I have this array to store image recognized:
int _detectedImages[NSMaxNumDetections ][NSPatchSize * NSPatchSize];
I call the C++ function this way:
numberOfDetections = nativeDetect(_resultImages);
And with the following code, I try to convert data from _detectedImages to UIImage.
for (int index = 0; index < numberOfDetections; index++)
{
if (_resultImages[index] != nil)
{
NSData* imageData = [NSData dataWithBytes:&_resultImages[index] length:sizeof(_resultImages[index])];
NSLog(#"##### Image data size: %d", [imageData length]);
UIImage* newImage = [UIImage imageWithData:imageData];
_lastDetectedSignImages[index] = newImage;
}
}
On the log I get this:
#### Image data size: 2304
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM setObject:atIndex:]: object cannot be nil'
*** First throw call stack:
newImage is nil but imageData has data.
Am I doing something wrong when I try to get an UIImage from that data?
The other possibility if that my C++ recognition software is doing something wrong, but I'm not going to show you that code here (sorry, it's copyrighted).
Or I'm not passing _resultImages correctly to nativeDetect.
Almost for sure your C++ images are in some bitmap format - RGBA etc. So your first task is to figure out what that format is (and the byte order too!). With that information you can use CGImageCreate() to create a CGImageRef. With that you can create a UIImage.