I'm trying to lead a TIFF image into a CGImageSource using CGImageSourceCreateWithURL. My URL is correct, is %20 encoded, is a path to an existing file, that file is a TIFF. However, the line:
NSString *fullPath = [[[fileNames objectAtIndex:pageNum - 1] string]
stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSString* urlStr = [NSString stringWithFormat: #"file:/%#", fullPath];
NSURL * url = [NSURL URLWithString: fullPath];
CGImageSourceRef src = CGImageSourceCreateWithURL (url, NULL);
gives me the following error:
Wed Aug 4 20:17:20 Brians-mini.local DocKeep[17199] <Error>: CGImageSourceCreateWithURL: CFURLCreateDataAndPropertiesFromResource failed with error code -15.
anyone know what error -15 is? or where I can find it?
thanks
ETA: I should also mention that I don't have this problem if the path doesn't have spaces in it... (Spaces in filenames are an abomination unto the lord, but I suppose we have to live with them... B-)
Don't use +URLWithString: for this. Instead use the purpose-built +fileURLWithPath:
Try this:
NSString *fullPath = [[fileNames objectAtIndex:pageNum - 1] string];
CFStringRef fullPathEscaped = CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)pdfPath, NULL, NULL,kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithString(NULL, fullPathEscaped, NULL);
CGImageSourceRef src = CGImageSourceCreateWithURL (url, NULL);
Error code -15 is is kCFURLImproperArgumentsError.
More info at Apple documentation for Core Foundation URL Access Utilities Reference.
Related
I have downloaded and added the following category into my project: https://github.com/nicklockwood/Base64
NSString *secret = #"7pgj8Dm6";
NSString *decodedSecret = [secret base64DecodedString];
NSLog(#"decoded Secret = %#", decodedSecret);
This however, always turns out to be nil. What is happening here ?
I hope it will helpful to you
NSString *decodeString = #"Raja";
Encode String
NSData *encodeData = [decodeString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [encodeData base64EncodedStringWithOptions:0];
DLog(#"Encode String Value: %#", base64String);
Decode String
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
DLog(#"Decode String Value: %#", base64String);
Thanks
I've failed to relax iOS native base64 decoder enough with NSDataBase64DecodingIgnoreUnknownCharacters to process jwt payloads
so I had to revert to a 3rdparty MF_Base64Codec
Sadly you might want to consider doing the 3rdparty route
until Apple will have fixed the bug or relaxed the decoder further to
accept what it currently considers crap input
Your input 7pgj8Dm6is an Invalid base64 encoded string. So the result will be always nil since the method base64DecodedString used only for UTF8 encoding.
As of iOS 7 and Mac OS 10.9, this library is not longer needed.
You can use initWithBase64EncodedString:options:
The default implementation of this method will reject non-alphabet characters, including line break characters. To support different encodings and ignore non-alphabet characters, specify an options value of NSDataBase64DecodingIgnoreUnknownCharacters.
NSString *path =[[NSBundle mainBundle]pathForResource:#"1025626909" ofType:#"txt"];
NSStringEncoding enc;
NSError *error;
NSString *pageSource = [[NSString alloc] initWithContentsOfFile:path usedEncoding:&enc error:&error];
NSLog(#"==%#",pageSource);
NSLog(#"==%#",[error description]);
above code output this:
2013-09-16 17:40:08.843 encodingTest[3350:c07] ==(null)
2013-09-16 17:40:08.846 encodingTest[3350:c07] ==Error Domain=NSCocoaErrorDomain Code=264 "The operation couldn’t be completed. (Cocoa error 264.)" UserInfo=0x71a5af0 {NSFilePath=/Users/dayu/Library/Application Support/iPhone Simulator/6.1/Applications/A33DE8D6-D64B-4791-ADB6-1AB4B35B743A/encodingTest.app/1025626909.txt
I can't get the txt file's content,hope superman come help me 。。。。。
I find the file I found this file encoding is GB2312 by other means,then i used the kCFStringEncodingGB_2312_80 code . There is no effect 。。。
Error 264 means NSString couldn't determine the encoding of the file; therefore you need to tell it what the encoding is, which is probably UTF-8:
NSString *pageSource = [[NSString alloc] initWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error];
In order to find what Error 264 means I looked in FoundationErrors.h:
NSFileReadUnknownStringEncodingError NS_ENUM_AVAILABLE(10_5, 2_0) = 264, // Read error (string encoding of file contents could not be determined)
Which I found in my Xcode app contents using the find command:
$ cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform
$ find . -name \*.h -exec fgrep -l 264 {} \;
./Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/AVFoundation.framework/Headers/AVAssetExportSession.h
(lots of others deleted)
./Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/FoundationErrors.h
I think the problem is the parameter of usedEncoding may use NSUTF8StringEncoding,then you may get the txt file's content.
let me tell why。。。because I hope NSLog all content of the txt file once. That always return null. so I take a small part of the txt file ,then I get the right content 。。。You know, when I see this result I was very happy .
NSStringEncoding enc2 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *textFile = [NSString stringWithContentsOfFile:path encoding:enc2 error:&error];
When calling +[NSURL URLWithString:] I have two options for building my URLs:
[[#"http://example.com" stringByAppendingPathComponent:#"foo"] stringByAppendingPathComponent:#"bar"]
or
[#"http://example.com" stringByAppendingFormat:#"/%#/%#",#"foo",#"bar"];
-[NSString stringByAppendingPathComponent:] seems like the more correct answer, but do I lose anything using -[NSString stringByAppendingFormat:] besides handling double-slashes as in the following case?
// http://example.com/foo/bar
[[#"http://example.com/" stringByAppendingPathComponent:#"/foo"] stringByAppendingPathComponent:#"bar"]
// http://example.com//foo/bar oops!
[#"http://example.com/" stringByAppendingFormat:#"/%#/%#",#"foo",#"bar"];
As you're working with URLS, you should use the NSURL methods:
NSURL * url = [NSURL URLWithString: #"http://example.com"];
url = [[url URLByAppendingPathComponent:#"foo"] URLByAppendingPathComponent:#"bar"]
or in Swift
var url = NSURL.URLWithString("http://example.com")
url = url.URLByAppendingPathComponent("foo").URLByAppendingPathComponent(".bar")
I just ran into a problem with stringByAppendingPathComponent: it removes double slashes everywhere!:
NSString* string1 = [[self baseURL] stringByAppendingString:partial];
NSString* string2 = [[self baseURL] stringByAppendingPathComponent:partial];
NSLog(#"string1 is %s", [string1 UTF8String]);
NSLog(#"string2 is %s", [string2 UTF8String]);
for a baseURl of https://blah.com
and a partial of /moreblah
Produces the two strings:
2012-09-07 14:02:09.724 myapp string1 is https://blah.com/moreblah
2012-09-07 14:02:09.749 myapp string2 is https:/blah.com/moreblah
But for some reason my calls to blah.com to get resource work with the single slash. But it indicates to me that stringByAppendingPathComponent is for paths - NOT urls.
This is on iPhone 4 hardware running iOS 5.1.
I outputted the UTF8 strings as I wanted to make sure that the debugger output I was seeing was believable.
So I guess I am saying - don't use path stuff on URLs, use some home brew or a library.
How about:
[NSString pathWithComponents:#[#"http://example.com", #"foo", #"bar"]]
As pointed out in the comments a / gets stripped from protocol when using the methods from NSPathUtitlites.h, so that is the obvious downfall. The solution I could come up with that is closest to the original one I posted is:
[#[ #"http://example.com", #"foo", #"bar" ] componentsJoinedByString:#"/"]
You will just need to use a literal for the path separator which is what NSString does.
NSString represents paths generically with ‘/’ as the path separator
and ‘.’ as the extension separator.
the point of stringByAppendingPathComponent is to handle double slashes, however, you could do something like:
[[#"http://example.com/" stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/%#", #"foo", #"bar"]]
I am trying to create a file by fopen and then write it, but weird things happened.
When I plug in the iphone to the usb port. Everything works fine. A file is created at the tmp directory or the document directory as expected.
When I plug off the device and do the same thing, the file did not appear. I was wondering why.
I use fopen to create the file. In my case, I should do this to create and then write the file. The call is fopen(pcm_output, "wb+");
You need to use this call.
char const *path = [fileManager fileSystemRepresentationWithPath:url.path];
From the docs...
fileSystemRepresentationWithPath:
- (const char *)fileSystemRepresentationWithPath:(NSString *)path
iOS (2.0 and later)
Returns a C-string representation of a given path that properly encodes Unicode strings for use by the file system.
path: A string object containing a path to a file.
A C-string representation of path that properly encodes Unicode strings for use by the file system.
You're probably writing outside of the sandbox, can you post the path?
Just as a test try to turn on iTunes Sharing (this should have no effect, it's just a test) for your app.
EDIT:
After testing I discovered that you have to use:
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docsPath stringByAppendingPathComponent:[NSString stringWithCString:pcm_output encoding:NSUTF8StringEncoding]];
fopen([filePath UTF8String], "wb+");
Instead of just:
fopen([filePath UTF8String], "wb+");
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask, YES
);
NSString* docDir = [paths objectAtIndex:0];
_tempLogPath = [docDir stringByAppendingPathComponent: #"Aisound5_CBLog.log"];
_tempPcmPath = [docDir stringByAppendingPathComponent: #"OutPcm.pcm"];
_tempWavPath = [docDir stringByAppendingPathComponent: #"OutWav.wav"];
tts_resource = [[bundle pathForResource:#"Resource_dev" ofType:#"irf"] UTF8String];
tts_log = [_tempLogPath UTF8String];
pcm_output = [_tempPcmPath UTF8String];
wav_output = [_tempWavPath UTF8String];
The original code is this, in which tts_resource tts_log pcm_output and wav_output are defined in a .h file and used in a .c file with fopen.
I had tried in your way to init the const string with the explicit const char* style, but the problem remains the same.
I'm just starting Objective C after being pampered with Applescript, and I can't seem to get FSMountServerVolumeSync to work. This is going to seem like a completely beginner question, but how do you pass a parameter from a variable to this action?
Let me explain:
I want to take a variable called *username and set it to the username in this action. I would also like to do this to *url and url. Is there any way someone could show me a sample of how to set this up, from an absolute beginner standpoint?
I am currently reading through tutorials and etc., but I would like to get this section of code done even if I don't exactly understand what I'm doing. ;)
Thanks in advance!
[edit] Here's what I've got so far:
- (IBAction)signin:(id)sender{
NSString * user = #"myusername";
NSString * password = #"mypassword";
NSURL * url = [NSURL URLWithString: #"smb://123.456.789.0"];
NSURL * mountDir = [NSURL URLWithString: #"/Students"];
OSStatus FSMountServerVolumeSync (
CFURLRef url,
CFURLRef mountDir,
CFStringRef user,
CFStringRef password,
FSVolumeRefNum *null,
OptionBits flags);
}
These aren't dumb questions at all.
Remember that CFStringRef and CFURLRef are toll free bridged, which means that the Objective C equivalents are NSString and NSURL. All you need to do is cast.
- (IBAction)signin:(id)sender{
NSString * user = #"myusername";
NSString * password = #"mypassword";
NSURL * url = [NSURL URLWithString: #"smb://123.456.789.0"];
NSURL * mountDir = [NSURL URLWithString: #"/Students"];
OptionBits flags = 0;
OSStatus err = FSMountServerVolumeSync (
(CFURLRef) url,
(CFURLRef) mountDir,
(CFStringRef) user,
(CFStringRef) password,
NULL,
flags);
if(err != noErr)
NSLog( #"some kind of error in FSMountServerVolumeSync - %ld", err );
}
See what I mean so far?
Here is some Apple documentation on toll free bridged types.