nsdata

Is there a practical way to compress NSData?

China☆狼群 提交于 2019-11-27 13:10:17
问题 I haven't seen any documentation on the topic, but that doesn't mean it doesn't exist. 回答1: Yes, compress the data with zlib. @Brad Larson posted on this: see here and added the code as well. There is a CocoaPod which uses Objective-Zip by flyingdolphinstudio. 回答2: Following @Zaph & @Brad Larson's posts, below are the 2 methods gzipInflate and gzipDeflate that work just fine to compress/decompress NSData . (code reformatted from cocoadev.com/wiki/NSDataCategory #import "zlib.h" // don't

Determine MIME Type of NSData Loaded From a File

岁酱吖の 提交于 2019-11-27 12:10:45
For various reasons I am intercepting http requests and loading content from files in my app's document directory using NSURLProtocol. Part of the process involves loading an NSData object, which could be anything from an html file to a jpeg image. NSURLProtocol requires setting a mimetype. Is there an API in the iPhone-SDK to determine the mimetype of the file or NSData content? Alexsander Akers Perhaps you could download the file and use this to get the file's MIME type. + (NSString*) mimeTypeForFileAtPath: (NSString *) path { if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {

Storing CLLocationCoordinates2D in NSMutableArray

廉价感情. 提交于 2019-11-27 11:43:02
问题 After some searching, I got the following solution : reference. CLLocationCoordinate2D* new_coordinate = malloc(sizeof(CLLocationCoordinate2D)); new_coordinate->latitude = latitude; new_coordinate->longitude = longitude; [points addObject:[NSData dataWithBytes:(void *)new_coordinate length:sizeof(CLLocationCoordinate2D)]]; free(new_coordinate); And access it as: CLLocationCoordinate2D* c = (CLLocationCoordinate2D*) [[points objectAtIndex:0] bytes]; However, someone claims that there is a

converting NSDictionary object to NSData object and vice-versa

人走茶凉 提交于 2019-11-27 11:36:46
I have to convert an NSDictionary object into NSData and further I have to get the same NSDictionary out of the NSData object. How should I go about it? use NSKeyedArchiver To convert NSDictionary To NSData NSMutableData *data = [[NSMutableData alloc]init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data]; [archiver encodeObject:YOURDICTIONARY forKey: YOURDATAKEY]; archiver finishEncoding]; [data writeToFile:YOURFILEPATH atomically:YES]; [data release]; [archiver release]; To get the NSDictionary back from the stored NSData NSData *data = [[NSMutableData

Is there a writeToFile equivalent for Swift 3's 'Data' type?

£可爱£侵袭症+ 提交于 2019-11-27 11:29:25
问题 I've some code which returns the new iOS 10 / Swift 3 NSData replacement(?) type: Data if let jpegData = UIImageJPEGRepresentation(newImage, 0.8) { ... } I want to write this image to disk, however NSData's writeToFile: method is not present for this class. It does have a writeToURL: method, but that doesn't seem to work with a file path (and appending component). Can anyone clarify how I would now do this, as used to be the case in Swift 2: jpegData.writeToFile(imagePath, atomically: true)

Encrypted NSData to NSString in obj-c?

痴心易碎 提交于 2019-11-27 10:58:21
I have an iPhone app which encrypts an inputted NSString using CCCrypt (AES256) and a plaintext key. The string and key are given to the encryption method which returns an NSData object. Requesting [data description] where 'data' is the encrypted string data gives an NSString like: "<0b368353 a707e7de 3eee5992 ee69827e e3603dc2 b0dbbc0b 861ca87d f39ce72a>" but when I try to convert that to an NSString, I get "(null)". I need to return an NSString to the user, which can be used to decrypt back to the original string using the same plaintext key. If the 'description' property of the NSData

Slow start for AVAudioPlayer the first time a sound is played

非 Y 不嫁゛ 提交于 2019-11-27 10:57:00
I'm trying to eliminate startup lag when playing a (very short -- less than 2 seconds) audio file via AVAudioPlayer on the iPhone. First, the code: NSString *audioFile = [NSString stringWithFormat:@"%@/%@.caf", [[NSBundle mainBundle] resourcePath], @"audiofile"]; NSData *audioData = [NSData dataWithContentsOfMappedFile:audioFile]; NSError *err; AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:audioData error:&err]; audioPlayer.delegate = self; [audioPlayer play]; I also implement the audioPlayerDidFinishPlaying method to release the AVAudioPlayer once I'm done.

Create an Array in Swift from an NSData Object

时光毁灭记忆、已成空白 提交于 2019-11-27 10:47:23
问题 I'm trying to store an array of integers to disk in swift. I can get them into an NSData object to store, but getting them back out into an array is difficult. I can get a raw COpaquePointer to the data with data.bytes but can't find a way to initialize a new swift array with that pointer. Does anyone know how to do it? import Foundation var arr : UInt32[] = [32,4,123,4,5,2]; let data = NSData(bytes: arr, length: arr.count * sizeof(UInt32)) println(data) //data looks good in the inspector //

NSData and UIImage

旧街凉风 提交于 2019-11-27 10:14:10
问题 I am trying to load UIImage object from NSData , and the sample code was to NSImage , I guess they should be the same. But just now loading the image, I am wondering what's the best to troubleshoot the UIImage loading NSData issue. 回答1: UIImage has an - initWithData: method. From the docs: "The data in the data parameter must be formatted to match the file format of one of the system’s supported image types." 回答2: I didn't try UIImageJPEGRepresentation() before, but UIImagePNGRepresentation

Using ALAssetsLibrary and ALAsset take out Image as NSData

痞子三分冷 提交于 2019-11-27 10:11:04
问题 I wish to extract the image using ALAssetsLibrary and ALAsset directly in the form of a NSData object. Using a NSURL I take out the image in the following manner. NSURL *referenceURL =newURL; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) { UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]; } Now here we take the image as a UIImage, but I need to take the