nsdata

Display PDF in UIWebView using loadData

混江龙づ霸主 提交于 2019-12-03 11:42:56
I am trying to display a PDF I have stored locally in a UIWebView. This is how I currently attempt to do this: if (![[NSFileManager defaultManager] fileExistsAtPath:self.url]) { LOG_ERROR(@"Couldn't load local file. File at path: %@ doesn't exist", self.url); return; } nsurl=[NSURL fileURLWithPath:self.url]; NSData *data = [NSData dataWithContentsOfFile:self.url]; LOG_DEBUG(@"data length:%d",[data length]); [self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil]; I have also tried passing nil for textEncoding, as well as using UIWebView's loadRequest. The

Best way to generate NSData object with random bytes of a specific length?

Deadly 提交于 2019-12-03 11:29:23
问题 If I create a new NSData object of a specific size using dataWithBytes:length:, what is the most efficient way to create the input bytes (20 Mb worth) of random characters, preferably without reading the data in from a file? I need a unique buffer of a specific size each time. Thanks 回答1: You can create a 20*2^20b NSData object, then append a random 4 byte integer to it 20*2^20/4 times with arc4random() . I believe you need to include stdlib.h (via Generating random numbers in Objective-C).

game state singleton cocos2d, initWithEncoder always returns null

前提是你 提交于 2019-12-03 10:15:59
I'm trying to write a basic test "game state" singleton in cocos2d, but for some reason upon loading the app, initWithCoder is never called. Any help would be much appreciated, thanks. Here's my singleton GameState.h: #import "cocos2d.h" @interface GameState : NSObject <NSCoding> { NSInteger level, score; Boolean seenInstructions; } @property (readwrite) NSInteger level; @property (readwrite) NSInteger score; @property (readwrite) Boolean seenInstructions; +(GameState *) sharedState; +(void) loadState; +(void) saveState; @end ... and GameState.m: #import "GameState.h" #import "Constants.h"

UIImage from bytes held in NSString

好久不见. 提交于 2019-12-03 10:14:41
问题 I am trying to create a UIImage from a byte array that is actually held within a NSString. Can someone please tell me how I can do that? Here is what I was thinking of doing: NSString *sourceString = @"mYActualBytesAREinHERe="; //get the bytes const char *bytesArray = [sourceString cStringUsingEncoding:NSASCIIStringEncoding]; //build the NSData object NSData *data = [NSData dataWithBytes:bytesArray length:[sourceString length]]; //create actual image UIImage *image = [UIImage imageWithData

implementing AES256 encryption into IOS

别等时光非礼了梦想. 提交于 2019-12-03 09:39:22
This is my java code. Now I want to implement same functionality in Objective-C. Cipher encryptCipher; IvParameterSpec iv = new IvParameterSpec(key); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = encryptCipher.doFinal(dataToEncrypt.getBytes()); Log.d("TAG", "encrypted string:" + Base64.encodeToString(encrypted, Base64.DEFAULT)); return Base64.encodeToString(encrypted, Base64.DEFAULT).trim(); This is my iOS implementation - (NSData *

convert NSData Length from bytes to megs

吃可爱长大的小学妹 提交于 2019-12-03 08:10:56
问题 I am trying to NSLog the number of megs my NSData object is however currently all I can get is bytes by using NSLog(@"%u", myData.length); So how would I change this NSLog statement so I can see something like 2.00 megs any help would be appreciated. 回答1: There are 1024 bytes in a kilobyte and 1024 kilobytes in a megabyte, so... NSLog(@"File size is : %.2f MB",(float)myData.length/1024.0f/1024.0f); Mind you, this is a simplistic approach that couldn't really properly accommodate for byte

Converting NSdata to bytes & then converting the first n bytes to int

南笙酒味 提交于 2019-12-03 08:02:29
问题 I've a NSData object. First I want to convert NSData object to bytes, then read the first four bytes in this NSData object and then convert the first four bytes to their equivalent integer values. Any ideas on how to go about this? How about converting all the four bytes to a single positive integer value? 回答1: Getting an int out of raw data is ambiguous: where does the data come from? What size do you want your int? Do you want them signed or unsigned? What byte ordering do you expect? So

How to convert a NSData to pdf in iPhone sdk?

。_饼干妹妹 提交于 2019-12-03 08:02:17
问题 Am converting a webpage as a pdf file. I did the following, NSString *string=[NSString stringWithFormat:@"%@.pdf",[chapersArray objectAtIndex:pageIndex]]; [controller1 addAttachmentData:pdfData mimeType:@"application/pdf" fileName:string]; [self presentModalViewController:controller1 animated:YES]; [controller1 release]; Now how can i convert my NSData into pdf and save in my application memory? Kindly help me with sample codes or suggestions. Thanks all. 回答1: Am assuming you have your pdf in

How to Load NSData into an AVPlayerItem?

╄→гoц情女王★ 提交于 2019-12-03 07:59:05
This is a follow-on question from my post about How to Encrypt mp3 Files in iOS . How would I go about loading a track into an AVPlayerItem using NSData rather than a file path/URL? Thanks [/*name of NSData*/ writeToFile:/*save file path*/ atomically: YES]; NSURL *filepath = [NSURL fileURLWithPath:/*save file path*/]; AVPlayerItem *player = [AVPlayerItem playerItemWithURL:filepath]; Untested, but should work. Updating Dustin's answer for Swift 5: var audioData: Data // some audio data var fileURL: URL // some local path, probably appending NSTemporaryDirectory() try! audioData.write(to:

How to pack struct in NSData? [duplicate]

五迷三道 提交于 2019-12-03 07:24:33
Possible Duplicate: Send and receive NSData via GameKit I have struct which consists of int variable and 2 float pointers (arrays). How can I pack this struct ib NSData and later unpack it? You can pack the structure using dataWithBytes method pf NSData : struct aStruct { /* Implementation */ }; //Struct variable aStruct exampleStruct; // pack the struct into an NSData Object NSData *myData = [NSData dataWithBytes:&exampleStruct length:sizeof(exampleStruct)]; // get back the the struct from the object [myData getBytes:&exampleStruct length:sizeof(exampleStruct)]; 来源: https://stackoverflow.com