Can't run animation with assets in iOS Swift - ios

I have animation Lottie and i can't run animation with assets. Animation playing but without assets.
I put my assets in this path. Web player nice playing. But in iOS in console print: -[LOTLayerContainer _setImageForAsset:]: Warn: image not found: img_0.png. For all images so.
Swift code:
let animation = LOTAnimationView(name: "data")
animation.frame = self.view.frame
animation.contentMode = .scaleAspectFill
self.view.addSubview(animation)
animation.play()
Path:

for solve this reason it needs to add the json files to copy bundle resources.
check the following and if your json file is not there add it there.
choose project-> choose the aim target -> goto Build Phases -> check Copy Bundle Resources

You need to change
animation = LOTAnimationView(name: "data")
to
animation = AnimationView(name: "data")

Related

Using an existing database in an iMessage app (Swift)

Currently building an iMessage app, and would like to experiment with using a database. I have a database that I would like to use in the app, and have included it in my project, and verified the target membership is correct. Using SQLite.Swift.
Whenever I try opening the connection to the database in simulator, I always get an error (unexpected nil) for the path of the database.
I've tried an image file the same way with no avail.
let imagePath = Bundle.main.path(forResource: "db", ofType: ".sqlite")
do {
let db = try Connection(imagePath!, readonly: true)
} catch {
}
I believe the issue is more related to what an iMessage "app" is - which is actually an extension, not a true app. There's no initial VC, thus no real Bundle.main to get to.
One (maybe soon a second) app of mine has a Photo Editing Extension - basically what I always have called a "shell connection" to an Apple app. You really have either a "do nothing" app with a connection to one of their apps, or you have a stand-alone app an share the code with the extension.
My solution for sharing code is to use a Framework target. Yes, a third project. (App, extension, shared code.) I found a technique that I think should work for you - basically, for images, scripts (my apps use .cikernel files) you add them into the framework project and return what you need in a function call.
You may be able to streamline this with a need for a Framework target. YMMV. The basics are this:
Someplace in Xcode you have a "Bundle Identifier". Something like *"com.company.projectname".
Put your files into a folder, maybe on your desktop. Add an extension to this folder called ".bundle". macOS will give you a warning, accept it. All you are really doing is creating your bundle.
Drag this into your Xcode project.
Code to get to this bundle, and the files inside it. (I'm not sure if need a framework here - try to drag this into your "MessagesExtension" target first.
So lets say you have images you wish to share between projects, extensions, whatever. After moving them into a folder called "images", andrenaming the folder with a ".bundle" at the end, and finally dragging it into your Xcode project, you pretty much need to add this function:
public func returnImage(_ named:String) -> UIImage {
let myBundle = Bundle.init(identifier: "com.company.project")
let imagePath = (myBundle?.path(forResource: "images", ofType: "bundle"))! + "/" + named
let theImage = UIImage(contentsOfFile: imagePath)
return theImage!
}
For a text file you want:
public func returnKernel(_ named:String) -> String {
let myBundle = Bundle.init(identifier: "com.company.project")
let kernelPath = (myBundle?.path(forResource: "cikernels", ofType: "bundle"))! + "/" + named + ".cikernel"
do {
return try String(contentsOfFile: kernelPath)
}
catch let error as NSError {
return error.description
}
}
Usage, for an image called "Camera.png" which is part of a bundle called "images.bundle":
let cameraImage = returnImage("Camera")
Since I don't work with SQLite files I don't have the exact code, but I think this should work. Remember to change "com.company.project" to what you have for the bundle identifier.

Does using a path from system, while calling a file, cause crashes?

I've a text file that stores my app's data for me.
If I use system path (/Users/username/Desktop/ProjectFile/data.txt), can it cause errors when application compiled into .ipa file?
open the file in your project, and check the target membership, as shown in following image. If target membership icon is checked for your corresponding target, then it will not make any problem during compilation(creating ipa). But if target membership is unchecked for your target build, then it will make the problem.
Using path form system like in the question causes crash when tested on a real iOS device.
Instead of using system path, using the code below will be more advantageous. It prevents crashes on real device.
let path = NSBundle.mainBundle().pathForResource("file", ofType: "txt")
let fileMgr = NSFileManager.defaultManager()
var array: [String] = []
if fileMgr.fileExistsAtPath(path!) {
do {
let text = try! String(contentsOfFile: path!, encoding: NSASCIIStringEncoding)
array = text.componentsSeparatedByString("|#|")
}
}

App resources not available when UI testing in Xcode 7

I'm trying to extend the new UI testing functionality in Xcode 7 by snapshotting the current screen elements (labels, images, buttons) and saving their accessibility information to json files.
The idea is that when running the UI tests later, a current screen snapshot can be taken and compared to the existing one, the test will fail if additional or missing elements are found.
Unfortunately the app resources don't seem available during UI testing, even with the correct target, so the json files can't be loaded for comparison. The following standard code fails to load a resource:
guard let resourcePath = NSBundle.mainBundle ().pathForResource ("StartScreenShapshot", ofType:"json") else {
XCTFail ("can't load resource StartScreenShapshot")
return
}
I can understand why Apple have taken this sandbox approach, as UI testing should be based on what's happening on the screen, and access to the workings of app shouldn't be needed, but not having access to the resource bundle is a pain.
So is there a way to load local resources from the app, or some other way locally, during Xcode 7 UI testing?
Saving the files locally (automatically) would also be a huge plus, would save creating them manually.
Thanks to #sage444
For unit testing the mainBundle() method doesn't work for retrieving a resource path, using a class does.
guard let resourcePath = NSBundle (forClass: self.dynamicType).pathForResource (contentName, ofType:"json") else {
XCTFail ("can't load resource \(contentName)")
return
}
Thanks #danfordham
Updated for Swift 3
1) Copy bundle resources
2) Reference new bundle this way,
guard let path = Bundle(for: type(of: self)).path(forResource: contentName, ofType: "json") else {
XCTFail ("can't load resource \(contentName)")
return
}

loading user images works in simulator but not on the iphone xcode 6 Swift

Im trying to load(and upload) images in my app(by picture path).
This is working in my simulator. everything works there. only when im trying to do this on the iphone itself it won't work and i see just the empty UIImageviews.
The paths are loaded the same as in the simulator. And originate from:
PHImageManager.defaultManager().requestImageDataForAsset(asset, options: nil)
{
imageData,dataUTI,orientation,info in
cell.setString(info["PHImageFileSandboxExtensionTokenKey"] as String,name:dataUTI as String)
}
And the PHImageFileSandboxExtentionTokenKey is split into the data and the url when loading the image.
this results in the simulator as :
/Users/Twizzler/Library/Developer/CoreSimulator/Devices/3E671415-8B83-44DA-870F-19BF2BC11F8F/data/Containers/Data/Application/8872109F-3784-40EB-BEB6-4E9FDABE013D/Documents/1613945_10200645161051698_4122753901212984922_n.jpg
and in the iphone as:
/private/var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG
Im loading the image like this:
let image = UIImage(named: "/private/var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG")
cell.imageView.image = image
in this case i put the image url hardcoded (this is in the final app an array of images)
I don't get an error or stack trace. When placeing a breakpoint im seeing the image information in the same way as the simulator
as suggested by the answer im now trying to load them as follows:
let image = UIImage(contentsOfFile: "/private/var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG")
cell.imageView.image = image
This isn't working and i can't upload the files
That’s not how imageNamed works—as the documentation states, that looks for an image with the given name inside your app’s bundle. Try imageWithContentsOfFile.
Well i fixed it! after some puzzling days im now using this way to access upload and
cell.imageView.image = UIImage(data: array[indexPath.row][0] as NSData)
This shows the image. I can save the NSData in the CoreData and reload the image on this way.
To lighten the load on the system im using this:
cellData.checked = 1
var imageData = UIImageJPEGRepresentation(cell.imageView.image, 1)
self.array.append([imageData,cellData.imageData] )
let myObj : ViewControllerImagePicker = self.parentViewController as ViewControllerImagePicker
let textfield: UILabel = myObj.amountLabel! as UILabel
textfield.text = String(self.array.count )
cell.textLabel.alpha = 1
this code is being called when there is clicked on a cell. by setting the cellData.imageData i can recall the cells (when clicking back to the collection view)
I do not know if the way im doing it is correct or works with more that 10 files because of data usage problems. But in my case with a max of 6 selected pictures it works like a charm
The simulator works completely different than a real device when it comes to files, the app bundle and sandboxing.
For starters the simulator will let you write to and add or change files in your app bundle. iOS running on a real device won't let you do this.
You can't hardcode any paths in your app. Even if it works today or on your device in test mode that doesn't mean it will work on a released version. And even if it does it could stop working on any update. Apple several times has changed where things are stored and the path structure in the phone. And where you are allowed to write.
Make bundle calls to get the path locations of standard folders.
Use the assets library to load images from the photo library, or to save them there.
There are tons of resources and tutorials online on how to do this.

Swift playgrounds with UIImage

I am working with Xcode 6, and I'm trying to recreate the code demoed during session 401 "What's new in Xcode 6". I've added an image to Images.xcassets (called Sample) and within the playground file I'm trying to access this image, as demoed.
My code is as follows (like the demo):
var sample = UIImage(named: "Sample")
However, I can't get it to work like the demo. Am I missing something?
Look at the iOS Developer Library->Playground Help and search"Resource Files" and you will find the answer
1、Open the .playground
2、Show the Project navigator by selecting View > Navigators > Show Project Navigator.
3、Drag the images to the Resources
Like follow:
Open the .playground file in Finder.
Create a folder called Resources next to it.
Add any images you want to this folder.
In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.
Under 'Resource Path' choose 'Relative To Playground'
Click the folder icon underneath and choose the Resources folder created earlier.
You should now have a bundle that you can use with the standard NSImage(named:"filename_without_extension"):
Note: Because Xcode will frequently overwrite the .playground folder, I recommend using this method so the resources folder isn't getting constantly deleted and re-created.
I had some trouble with this also.
Unfortunately, Chris' answer didn't work for me. I suspect perhaps a later beta release of Xcode 6 may have removed this setting.
Here's a solution as of Xcode 6.0 beta 4 (6A267N) available 21st July 2014. I would guess that this corresponds to the "Inside playground" option previously. That is where the Resources folder is within the playground package itself.
Here's how to set this up.
Using Finder - or if you're like me and use the awesome Path Finder - right select and choose Show Package Contents as follows:
That reveals the packages Resources folder:
Copying the image files into that folder will do the business:
let imageNames = ["back-button", "background", "bg_top"]
let images = imageNames.map { UIImage(named: $0) }
For Xcode 9:
Select Resources folder
Right click then "Add files to "Resources""
Use it like:
let image = UIImage(named: "no")
As of Xcode 8 Beta 1, you can use Image Literals to import an image into an Xcode playground:
Start typing image to add an image literal:
Select (or browse for) your image:
See your image inline:
However, I can't get it to work like the demo. Am I missing something?
I'm not sure where you need to put the image to refer to it using only the name, but I got the same code to work by specifying the full path to the image, like:
var sample = UIImage(named: "/Users/my_user_name/Desktop/Image1234.jpg")
Having to use the full path seems more complicated than it should be, but it works and it let me move on to more interesting problems.
You can find out the path of resourcePath using these commands in playground:
var bundle = NSBundle.mainBundle()
var path = bundle.resourcePath
Default for me was:
/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents
I had difficulty getting this setup for an iOS playground, as opposed to an OS X playground. Trying to do it using bundles and relative paths makes it more complicated.
If you just want to get your hands on an image quickly, you can also just use absolute file path:
On iOS:
# iOS
let absoluteImagePath = "/absolute/path/to/image.png"
let image = UIImage(contentsOfFile: absoluteImagePath)
And on OS X
# OS X
let absoluteImagePath = "/absolute/path/to/image.png"
let image = NSImage(contentsOfFile: absoluteImagePath)
This is what worked for me on Xcode Version 6.1.1.
Create Playground file under same directory as main storyboard.
Open Utilities pane for Playground file, and click the right arrow in Resource Path section to add your images in that directory.
Test image within Playground file.
On the iOS playground with XCode 6 beta 5 (and probably later) you can see the image inside the bundle:
In the playground press Cmd+Opt+1 and click the arrow under the Resource Path (this will open Finder)
Put your picture to this folder
Access it with either
let img = UIImage(named: "logo.png", inBundle: NSBundle.mainBundle(),
compatibleWithTraitCollection: nil)
or
let path = NSBundle.mainBundle().pathForResource("logo", ofType: "png")
let img = UIImage(contentsOfFile:path)
or in Swift 4:
let path = Bundle.main.path(forResource:"logo", ofType: "png")
let img = NSImage(contentsOfFile:path!)
Using XCode 6.3.1 and running playground in full simulator (iOS) I had to do the following:
Find your .playground file in finder
Right click it -> show package contents
If it doesn't already exist, create a folder named Resources inside the package
Add your image there
Then just instantiate with
let i = UIImage(named: "filename.png")
I have no idea why it is not working the easy way with PDF images.
I had to add this extension to load PDF images into my Playground:
extension UIImage {
static func fromPDF(_ url: URL) -> UIImage? {
var image: UIImage?
guard
let doc = CGPDFDocument(url as CFURL),
let page = doc .page(at: 1)
else { return nil }
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
image = renderer.image { ctx in
UIColor.clear.set()
ctx.fill(pageRect)
ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
ctx.cgContext.drawPDFPage(page)
}
return image
}
}
// Given the file "myImage.pdf" was added to your Playground's Resources folder.
let image = UIImage.fromPDF(#fileLiteral(resourceName: "myImage.pdf"))
It is based on this: https://developer.apple.com/forums/thread/90990

Resources