Creating a USDZ file on the fly? - objective-c

Anyone aware of how to create usdz from obj on the fly? Our application creates an obj file using a third party library and for using the QLPreviewController we need to convert it to usdz format. There are ways to do that using the terminal but wondering if there is any way to do it programmatically?

An engineer on my team figured this out last week!
Creating USDZ files is funny right now - currently we can fake it by saving a USDC file and... renaming the extension!
First you'll want to load the .obj file at filePath as an MDLAsset
NSURL *url = [NSURL fileURLWithPath:filePath];
MDLAsset *asset = [[MDLAsset alloc]initWithURL:url];
ensure the MDLAsset can write the desired extensions
usdc is supported (USD binary format)
if([MDLAsset canExportFileExtension:#"usdc"]){
NSLog(#"able to export as usdc");
// save the usdc file
[asset exportAssetToURL:usdcUrl];
}
rename the usdc to usdz because that's all it takes
NSError *renameErr;
NSFileManager *fm = [[NSFileManager alloc] init];
BOOL mvResult = [fm moveItemAtPath:usdcPath toPath:usdzPath error:& renameErr];
if(! mvResult){
NSLog(#"Error renaming usdz file: %#", [renameErr localizedDescription]);
}
Hope this helps until Apple can give us a more thorough how-to.
If you want to read a more long form breakdown of this - https://www.scandy.co/blog/how-to-export-simple-3d-objects-as-usdz-on-ios

Related

Cocoa saving text files when distributing

In the app I'm designing I have a couple text fields that each save to a .txt file when the app is closed, and then read back in when the app opens up; however, when I try to distribute the app (just exporting it as an application and dropboxing it to some friends) the people I'm sending it to don't have the file locations that I'm saving to on their computers, so the text fields don't save. Is there a way I can create the .txt files on their computers when the app downloads? or is there a better way to save when distributing than doing .txt files? Thanks.
Why not just use NSUserDefaults and store the (hopefully fairly small) text in the app's preferences database? It's guaranteed to exist and be accessible without running afoul of sandbox restrictions or nonstandard storage locations.
Beyond that, you'll need to provide more information if you want a direct answer to your question. Specifically, what is the exact path you're using and what is the exact error you're receiving? Why do you feel you need to bundle empty text files when all it takes is creating the file(s) at runtime?
I would just package the initial .txt file in your application bundle. Check if the file exists on your user's local file system. If not, read your bundle's .txt file into a string, then save that string to the user's file system in the correct place.
NSError *fileError = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
//Read the file
NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&fileError];
if (fileError) {
//Handle the error
}
} else {
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"initialFile" ofType:#"txt"];
NSString *initialFileContents = [[NSString alloc] initWithContentsOfFile:bundlePath encoding:NSUTF8StringEncoding error:&fileError];
if (!fileError) {
[initialFileContents writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&fileError];
if (fileError) {
//Handle the error
}
}
}

writeToFile doesn't change the content of the file xcode4 objective-c

I finished writing a little program, which is able to read and write a/into a .txt file.
When I execute the program, everything is running fine except that the content of the file doesn't change permanently. I got a writeToFile and "readFile" button and the content seems to change every time I press one of them, but when I open the file manually (while testing or after shutting down the program) theres still the origin content in it.
Doesn't the "real" file content change while just using the simulator? Or is it just me making some bad mistakes?
-(IBAction)buttonPressed { //The writeToFile Method
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"txt"];
NSString *writeData = enterText.text;
NSError *error;
BOOL ok = [writeData writeToFile:filePath atomically:NO encoding:NSUnicodeStringEncoding error:&error];
if (!ok)
{
NSLog(#"Error while writing file at %#/n%#",filePath,[error localizedFailureReason]);
}
testText.text =#"File saved!";
enterText.text = #"";
enterText.placeholder =#"Enter your text here";
}
testText = TextView for Output
enterText = TextField for Input
Your filePath variable is pointing to a file within the resource bundle of your app (which is not writable). What you need to do is locate the user's Documents folder, and create your file there.

Zipping a folder in Objective C

Are there any libraries that work in Objective C for zipping entire folders (and decompressing them)? I have looked at some of them by searching but they look like they require adding files individually and some of them supposedly crash...
It looks like this library might work:
http://bitbucket.org/dchest/osxzip/overview
I don't know if it supports folders, however. Anyone know if it does or have any other libraries that support zipping folders? Even sample code for interacting with the command line libz would be fine with me...
You could use NSTask to run the command line ditto program. Be sure to look at the ditto man page for the right combination of flags to get Finder-compatible zipping.
According to this example: http://www.raywenderlich.com/1948/how-integrate-itunes-file-sharing-with-your-ios-app you can get a NSData Object with the Zipped Data and then just write it with [data writeToFile....]
- (NSData *)exportToNSData {
NSError *error;
NSURL *url = [NSURL fileURLWithPath:_docPath];
NSFileWrapper *dirWrapper = [[[NSFileWrapper alloc] initWithURL:url options:0 error:&error] autorelease];
if (dirWrapper == nil) {
NSLog(#"Error creating directory wrapper: %#", error.localizedDescription);
return nil;
}
NSData *dirData = [dirWrapper serializedRepresentation];
NSData *gzData = [dirData gzipDeflate];
return gzData;
}

TIFF image format for iphone application

I am working on iphone app and i need to save image into .tiff format.
It is possible to save image into png format using UIImagePNGRepresentation method and JPEG format using UIImageJPEGRepresentation. But i need to save signature captured by imageview into tiff format.
I unable to use NSImage class so that i can call TIFFRepresentation method.
How can i do it.Send me suggestion...
Thanks in advance...
I don't know what you mean by "captured by imageview". The means to save as TIFF wasn't introduced until iOS 4. Here is example code to save an arbitrary file as a TIFF; you can do this with any file-format data, no matter how you obtained it. You need to link to ImageIO framework and do #import <ImageIO/ImageIO.h>:
NSURL* url = [[NSBundle mainBundle] URLForResource:#"colson" withExtension:#"jpg"];
CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
NSFileManager* fm = [[NSFileManager alloc] init];
NSURL* suppurl = [fm URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES error:NULL];
NSURL* tiff = [suppurl URLByAppendingPathComponent:#"mytiff.tiff"];
CGImageDestinationRef dest = CGImageDestinationCreateWithURL((CFURLRef)tiff,
(CFStringRef)#"public.tiff", 1, NULL);
CGImageDestinationAddImageFromSource(dest, src, 0, NULL);
bool ok = CGImageDestinationFinalize(dest);
NSLog(#"result %i", ok); // 1, all went well
// and don't forget memory management, release source and destination, NSFileManager
[fm release]; CFRelease(src); CFRelease(dest);
Hmm, I don't develop for the iPhone, so can't tell you if there's a magic API way of doing this, but many years ago I had to create TIFF images manually.
The TIFF format is a bit whacky if you're used to just using a framework's CreateThisStuffAsAnImage() methods, but you can get the spec here and create the file yourself:
http://partners.adobe.com/public/developer/tiff/index.html#spec

File Handling in Objective C

Is there anyway to do Files Handling in Objective-C? I am just trying to do simple read and write and can use 'c' but i am force to use Objective-C classes for that :#. I am looking into NSInputStream, but its going over my head. Is there any tutorial which explains how to use NSInputStream?
I had trouble with basic file i/o when I first hit it in Obj-C as well. I ended up using NSFileHandle to get C style access to my file. Here's a basic example:
// note: myFilename is an NSString containing the full path to the file
// create the file
NSFileManager *fManager = [[NSFileManager alloc] init];
if ([fManager createFileAtPath:myFilename contents:nil attributes:nil] != YES) {
NSLog(#"Failed to create file: %#", myFilename);
}
[fManager release]; fManager = nil;
// open the file for updating
NSFileHandle *myFile = [NSFileHandle fileHandleForUpdatingAtPath:myFilename];
if (myFile == nil) {
NSLog(#"Failed to open file for updating: %#", myFilename);
}
// truncate the file so it is guaranteed to be empty
[myFile truncateFileAtOffset:0];
// note: rawData is an NSData object
// write data to a file
[myFile writeData:rawData];
// close the file handle
[myFile closeFile]; myFile = nil;
If all you need to do is really simple I/O, you can just tell an object to initialize itself from, or write itself to, a filesystem path or URL. This works with several Foundation classes, including NSString, NSData, NSArray, and NSDictionary among others.
Try starting out by looking at the following two NSString methods:
- initWithContentsOfFile:encoding:error:
- writeToFile:atomically:encoding:error:
I find apple's guides short and to the point.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html