nsdata

Downloading a large file in iOS app

三世轮回 提交于 2019-12-06 01:51:26
I'm trying to clean up some existing code that downloads a large file from a server in chunks, checks the checksum on each 50 packets, then stitches them together. I'm having some trouble to see if it's the most efficient way as right now it crashes some time because of memory issues. If I don't have the checksum, it does not seem to crash, but I would prefer if I could check my files first. @property (nonatomic, retain) NSMutableData * ReceivedData; - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSData *sequencePacketData = [[NSData alloc] initWithData:self.ReceivedData];

Progress bar while downloading

元气小坏坏 提交于 2019-12-06 01:35:06
I have an app with in-app downloading. I successfully download mp3 files in this way: NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://.../somefile.mp3"]]; [data1 writeToFile:filePath atomically:YES]; But there is really big pause while this code is executing. How can I work out the progress of the download and display it with a progress bar? The problem is that dataWithContentsOfURL: is a blocking call. This means that it will block the thread that it is running on. You've got a couple of options to fix this, and the better one is probably to use an NSURLConnection

How to handle NSData and return UIImage (Swift)

和自甴很熟 提交于 2019-12-06 00:28:57
I am trying to retrieve an image from a URL from a backendless database & convert this into an image, and return the function with a UIImageView. How do I handle a NSError in this code, and still return the UIImageView? func getImage() -> UIView { let imageLink = backendless.userService.currentUser.getProperty("Avatar") let URL = NSURL(string: imageLink as! String) let data = NSData(contentsOfURL: URL!) let image: UIImage! image = UIImage(data: data!) return UIImageView(image: image!) } Updated my answer based on the comments. You can return a placeholder image if the one you are looking for

Store images in sqlite or just a reference to it?

这一生的挚爱 提交于 2019-12-05 23:09:09
I have made couple of apps using coredata and I was storing images in sqlite, but somewhere i found that it is bad. I've searched the net but all I've found is this suggestion: image size < 100kb store in the same table as the relevant data image size < 1mb store in a separate table attached via a relationship to avoid loading unnecessarily image size > 1mb store on disk and reference it inside of Core Data So my question is: what are pros and cons of saving an image in sqlite db as NSData , and storing just a reference to the image while image is saved in the file system? Apple provide some

NSAttributedString to NSData No visible @interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes:

霸气de小男生 提交于 2019-12-05 22:55:39
问题 I am trying to convert NSAttributedString to NSData using RTFDFromRange method. Getting this: No visible @interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes: What is wrong with my code? NSAttributedString *val=self.textview.attributedText; NSData *data = [val RTFDFromRange:NSMakeRange(0, self.textview.text.length) documentAttributes:nil]; 回答1: NSAttributedString does not have a method called RTFDFromRange for iOS, but only for Mac OS X. To convert

How do I convert NSString to NSData?

烂漫一生 提交于 2019-12-05 20:30:30
问题 I have this line of code to convert NSString to NSData: NSData *data = [NSData dataWithBytes:[message UTF8String] length:[message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; How do I do this in Unicode instead of UTF8? My message may contain cyrillic characters or diacritical marks. 回答1: First off, you should use dataUsingEncoding: instead of going through UTF8String . You only use UTF8String when you need a C string in that encoding. Then, for “Unicode” (specifically, UTF-16), just

How do I make my AFNetworking “responseObject” that I receive a NSDictionary I can parse?

拥有回忆 提交于 2019-12-05 18:16:22
I have this call with AFNetworking 1.0 that returns a responseObject with the data from the API that I want: [[AFDiffbotClient sharedClient] postPath:@"http://diffbot.com/api/batch" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { However, I have no idea how to process responseObject . If I check [responseObject class] I get NSData . If I NSLog(@"%@", responseObject) I get a bunch of numbers (memory addresses I assume): <5b0a7b22 68656164 65727322 3a5b7b22 6e616d65 223a6e75 6c6c2c22 76616c75 65223a22 48545450 2f312e31 20323030 204f4b22 7d2c7b22 6e616d65

How to convert Data of Int16 audio samples to array of float audio samples

心不动则不痛 提交于 2019-12-05 16:17:43
I'm currently working with audio samples. I get them from AVAssetReader and have a CMSampleBuffer with something like this: guard let sampleBuffer = readerOutput.copyNextSampleBuffer() else { guard reader.status == .completed else { return nil } // Completed // samples is an array of Int16 let samples = sampleData.withUnsafeBytes { Array(UnsafeBufferPointer<Int16>( start: $0, count: sampleData.count / MemoryLayout<Int16>.size)) } // The only way I found to convert [Int16] -> [Float]... return samples.map { Float($0) / Float(Int16.max)} } guard let blockBuffer = CMSampleBufferGetDataBuffer

convert base64 decoded NSData to NSString

烂漫一生 提交于 2019-12-05 13:30:19
I'm trying to encode and decode base64 data. but while decoding the base64 data, it returns bunch of hex values, but i couldn't display or printout using NSlog to the original readable strings. The below code couldn't print anything, just empty. Can anyone help ? thanks > > NSString* msgEncoded = [[NSString alloc] initWithFormat:@"Q1NNKE1DTC9TTUEgUkNWL2FkbWluQHNldGVjcy5jb20gT1JHLyBUVkIvNDNkYzNlMzQwYWQ3Yzkp:"]; NSData* decoded = [[NSData alloc] initWithData:[self decodeBase64WithString:msgEncoded]]; NSString* plainString = [[NSString alloc]initWithData:decoded encoding:NSUTF8StringEncoding];

How to deserialize json object and assign to a NSDictionary in iOS

我的未来我决定 提交于 2019-12-05 12:46:57
问题 This is my code for handling server response. - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"connectionDidFinishLoading : %@", [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]); } This is the message Server response to me, NSLog JSON displays in console. connectionDidFinishLoading : {"ErrorCode":"CssParameterException","ErrorMessage":"An error has occurred, please try again later.","Success":false} My question is: how do I deserialize the