nsdata

Read only a portion of a file from disk in objective-c

柔情痞子 提交于 2019-12-03 07:00:38
I am reading a very large file using a NSInputStream and sending them to a device in packets. If the receiver does not get a packet, I can send back to the sender with a packet number, representing the starting location in bytes of the packet missing. I know an NSInputStream cannot rewind and grab the packet, but is there another way to grab the requested byte range without loading the entire large file into memory? If there was a [NSData dataWithContentsOfFileAtPath:inRange] method, it would be perfect. You can rewind with NSInputStream: [stream setProperty:[NSNumber numberWithInt:offset]

Can I create an NSURL that refers to in-memory NSData?

给你一囗甜甜゛ 提交于 2019-12-03 06:38:20
问题 The docs for NSURL state that: An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data . I have a blob of in-memory data that I'd like to hand to a library that wants to load a resource via an NSURL . Sure, I can first write this NSData to a temp file and then create a file:// NSURL from that, but I'd prefer to have the URL point directly to the buffer that I

Read only “N” bytes from a file in Cocoa

血红的双手。 提交于 2019-12-03 05:02:56
问题 How to read only "N" bytes from a specified file? 回答1: If you want random access to the contents of the file in a manner similar to having loaded it via NSData but without actually reading everything into memory, you can use memory mapping. Doing so means that the file on disk becomes treated as a section of virtual memory, and will be paged in and out just like regular virtual memory. NSError * error = nil; NSData * theData = [NSData dataWithContentsOfFile: thePath options: NSMappedRead

How do I convert an NSNumber to NSData?

别来无恙 提交于 2019-12-03 04:57:01
I need to transmit an integer through GameKit using sendDataToAllPeers:withDataMode:error: but I don't know how to convert my NSNumber to NSData in order to send. I currently have: NSNumber *indexNum = [NSNumber numberWithInt:index]; [gkSession sendDataToAllPeers:indexNum withDataMode:GKSendDataReliable error:nil]; but obviously the indexNum needs to be converted to NSData before I can send it. Does anyone know how to do this please? Thanks! I would not recommend NSKeyedArchiver for such a simple task, because it adds PLIST overhead on top of it and class versioning. Pack: NSUInteger index =

iPhone - NSData from local file's URL

梦想的初衷 提交于 2019-12-03 04:05:00
问题 I have a NSURL object which gives me the path of a local file (in documents folder). I want to populate an NSData object with the contents of this file. Tried using dataWithContentsOfURL: but this fails. I know the file exists because the iPhone SDK returns the path. Can someone please tell me how I can get an NSData object from the URL of a local file? Thanks. 回答1: // Given some file path URL: NSURL *pathURL // Note: [pathURL isFileURL] must return YES NSString *path = [pathURL path]; NSData

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

徘徊边缘 提交于 2019-12-03 01:56:01
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 Thomson Comer 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 ). #include <stdlib.h> -(NSData*)create20mbRandomNSData { int twentyMb = 20971520; NSMutableData*

NSMutableData remove bytes?

故事扮演 提交于 2019-12-03 01:15:41
I can add bytes to a NSMutableData instance easily by using the appendData method, however I do not see any similar method for removing data? Am I overlooking something, or do I need to create a new object and copy over only the bytes I need? Please see the documentation of the following method: - (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)replacementBytes length:(NSUInteger)replacementLength Apple clearly says the following: If the length of range is not equal to replacementLength, the receiver is resized to accommodate the new bytes. Any bytes past range in the receiver

NSImage from a 1D pixel array?

有些话、适合烂在心里 提交于 2019-12-03 00:51:07
I have a large 1D dynamic array in my program that represents a FITS image on disk i.e. it holds all the pixel values of the image. The type of the array is double . At the moment, I am only concerned with monochrome images. Since Cocoa does not support the FITS format directly, I am reading in the images using the CFITSIO library. This works - I can manipulate the array as I wish and save the result to disk using the library. However, I now want to display the image. I presume this is something NSImage or NSView can do. But the class references don't seem to list a method which could take a C

convert NSData Length from bytes to megs

空扰寡人 提交于 2019-12-02 21:47:22
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. 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 sizes below 1,048,576 bytes or above 1,073,741,823 bytes. For a more complete solution that can handle varying

How to convert a NSData to pdf in iPhone sdk?

别来无恙 提交于 2019-12-02 21:31:28
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. Am assuming you have your pdf in documents directory here. You can change it to where ever it actually is. Try this - //to convert pdf to