nsdata

Read only “N” bytes from a file in Cocoa

给你一囗甜甜゛ 提交于 2019-12-02 19:22:37
How to read only "N" bytes from a specified file? 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 error: &error]; If you don't care about getting filesystem error details, you can just use: NSData * theData =

Memory leak NSAutoreleasePool

送分小仙女□ 提交于 2019-12-02 17:53:11
问题 With instruments i got a memory leak on this piece of code and i don't understand why! -(void)goToThisUrl:(id) targetUrl { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; if (someCondition) { // Doing some stuff here } // Instruments show memory leak on data else { NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: targetUrl]]; myTargetImage = [UIImage imageWithData:data]; // When releasing data(because data retainCount = 2), i got: // Incorrect decrement of the

parsing NSData object for information

£可爱£侵袭症+ 提交于 2019-12-02 17:43:39
问题 I have a NSData object coming back from my server, it varies in its content but sticks to a particular structure. I would like to know (hopfully with some example code) how to work though this object to get the data I need out of it. the structure of the data objects inside the objects are like this leading value ( UInt16 ) - (tells me what section of the response it is) Size of string ( UInt32 ) or number - ( UInt32 ) String (not null terminated) i.e. followed by the next leading value. I

How to Covert struct with an Array of string to NSData and vice versa Swift

不羁的心 提交于 2019-12-02 16:29:59
问题 I have this struct: struct MessageRandomWords { let message = MessageType.kMessageTypeRandomWords let randomWords : Array<Array<String>> } I'm trying to convert this struct to NSDate by doing this: var message = MessageRandomWords(randomWords: self.words) let data = NSData(bytes: &message, length: sizeof(MessageRandomWords)) But when i'm trying to convert this back to the original struct: var messageRandomWords : MessageRandomWords? data.getBytes(&messageRandomWords, length: sizeof

iPhone - NSData from local file's URL

做~自己de王妃 提交于 2019-12-02 16:27:36
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. // Given some file path URL: NSURL *pathURL // Note: [pathURL isFileURL] must return YES NSString *path = [pathURL path]; NSData *data = [[NSFileManager defaultManager] contentsAtPath:path]; To get this work you just need to do: NSURL

Cannot pass immutable value as inout argument: function call returns immutable value

微笑、不失礼 提交于 2019-12-02 12:01:13
I forked this project, so I am not as familiar with all of the details: https://github.com/nebs/hello-bluetooth/blob/master/HelloBluetooth/NSData%2BInt8.swift . This is all part of an extension of NSData that the I am using to send 8-bit values to an Arduino. func int8Value() -> Int8 { var value: Int8 = 0 copyBytes(to: &UInt8(value), count: MemoryLayout<Int8>.size) //BUG return value } However, it appears in Swift 3 that this now throws an error in the copyBytes section. Although I have seen some solutions such as passing an address in the parameter, I did not want to risk breaking the

Memory leak NSAutoreleasePool

六眼飞鱼酱① 提交于 2019-12-02 10:44:57
With instruments i got a memory leak on this piece of code and i don't understand why! -(void)goToThisUrl:(id) targetUrl { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; if (someCondition) { // Doing some stuff here } // Instruments show memory leak on data else { NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: targetUrl]]; myTargetImage = [UIImage imageWithData:data]; // When releasing data(because data retainCount = 2), i got: // Incorrect decrement of the reference count of an object that is not owned at this point by the caller //[data release]; } [pool

Objective-C: Getting PNG Thumbnail from Movie with NSData

▼魔方 西西 提交于 2019-12-02 10:30:43
I have the following code to attempt to get a screenshot of a video file from NSData. I can confirm the NSData is valid and not nil, however both dataString and movieURL are returning nil. - (UIImage *)imageFromMovie:(NSData *)movieData { // set up the movie player NSString *dataString = [[NSString alloc] initWithData:movieData encoding:NSUTF8StringEncoding]; NSURL *movieURL = [NSURL URLWithString:dataString]; // get the thumbnail AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:movieURL options:nil]; AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];

How to Covert struct with an Array of string to NSData and vice versa Swift

旧时模样 提交于 2019-12-02 09:51:51
I have this struct: struct MessageRandomWords { let message = MessageType.kMessageTypeRandomWords let randomWords : Array<Array<String>> } I'm trying to convert this struct to NSDate by doing this: var message = MessageRandomWords(randomWords: self.words) let data = NSData(bytes: &message, length: sizeof(MessageRandomWords)) But when i'm trying to convert this back to the original struct: var messageRandomWords : MessageRandomWords? data.getBytes(&messageRandomWords, length: sizeof(MessageRandomWords)) if let messageRandomWords = messageRandomWords { } I got a BAD_ACCESS erro on the if let

NSData cannot retrive image from internet anymore [duplicate]

Deadly 提交于 2019-12-02 09:48:13
This question already has an answer here: How do I load an HTTP URL with App Transport Security enabled in iOS 9? [duplicate] 8 answers The following code works fine before iOS8.4.1 (includ 8.4.1). While it ruturns nil in iOS9.0.1. Is it a bug or there is a public annoucement for this change? I tested with two iPads. let url = NSURL(string: "http://www.mapshots.com/wp-content/uploads/2014/05/mapshots-ag-studio-agricultural-mapping-software-logo.png") let data = NSData(contentsOfURL: url!) NSLog("Data length @%", (data?.length)!) Gandalf This is an issue related to ATS(App Transport Security