nsdata

Trying to Write NSString sha1 function, but it's returning null

主宰稳场 提交于 2019-11-30 07:38:02
问题 I have the following Objective-C function: +(NSString *)stringToSha1:(NSString *)str{ NSMutableData *dataToHash = [[NSMutableData alloc] init]; [dataToHash appendData:[str dataUsingEncoding:NSUTF8StringEncoding]]; unsigned char hashBytes[CC_SHA1_DIGEST_LENGTH]; CC_SHA1([dataToHash bytes], [dataToHash length], hashBytes); NSData *encodedData = [NSData dataWithBytes:hashBytes length:CC_SHA1_DIGEST_LENGTH]; [dataToHash release]; NSString *encodedStr = [NSString stringWithUTF8String:[encodedData

Swift - Create a GIF from Images and turn it into NSData

爱⌒轻易说出口 提交于 2019-11-30 07:36:27
This might be an amateur question, but although I have searched Stack Overflow extensibly, I haven't been able to get an answer for my specific problem. I was successful in creating a GIF file from an array of images by following a Github example: func createGIF(with images: [NSImage], name: NSURL, loopCount: Int = 0, frameDelay: Double) { let destinationURL = name let destinationGIF = CGImageDestinationCreateWithURL(destinationURL, kUTTypeGIF, images.count, nil)! // This dictionary controls the delay between frames // If you don't specify this, CGImage will apply a default delay let

How to convert CMSampleBufferRef to NSData

这一生的挚爱 提交于 2019-11-30 07:01:48
How do you convert CMSampleBufferRef to NSData? I've managed to get the data for an MPMediaItem by following Erik Aigner's answer on this thread , however the data is of type CMSampleBufferRef . I know CMSampleBufferRef is a struct and is defined in the CMSampleBuffer Reference in the iOS Dev Library, but I don't think I fully understand what it is. None of the CMSampleBuffer functions seem to be an obvious solution. Daniel Here you go this works for audio sample buffer which is what you are looking at, and if you want to look at the whole process (getting all audio data from MPMediaItem into

Does -dataWithContentsOfURL: of NSData work in a background thread?

孤者浪人 提交于 2019-11-30 06:50:25
Does -dataWithContentsOfURL: of NSData work in a background thread? Michael Kessler No, it doesn't. In order to get data from URL asynchronously you should use the NSURLRequest and NSURLConnection approach. You will have to implement the NSURLConnectionDelegate methods: -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; -(void)connectionDidFinishLoading:(NSURLConnection *)connection; -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; I'm

Compute a checksum on the iPhone from NSData

天大地大妈咪最大 提交于 2019-11-30 06:26:00
问题 Using the iPhone SDK, I'm having the user select images from the image picker. If the user selects an image they've selected before, I'd like to make the user aware of it. My initial plan (just to make sure other things work for now) is to save the image to a file (need to do this anyway for other reasons), using a checksum of the NSData as the filename. Then, when they select the same image later on, the checksum will be the same and so I can see that a file with that name already exists -

NSData contentsOfUrl returns nil

こ雲淡風輕ζ 提交于 2019-11-30 05:05:15
问题 I found several similar questions on StackOverflow but none of them solve my problem. I am trying to get a image from a url. Here's how I do it: let url = NSURL(string: "http://pic3.zhimg.com/8161ba9638273e0fb1a0201789d22d8e_m.jpg") let data = NSData(contentsOfURL: url!) let image = UIImage(data: data!) But I got an error telling me that data is nil. How can I solve this? Thanks. UPDATE Here's some screenshots of the error: 回答1: This is probably a result of Apple's new app transport security

Converting NSData to Integer in Swift

点点圈 提交于 2019-11-30 04:12:28
问题 In Objective-C the code looked liked this and worked flawlessly, NSInteger random = arc4random_uniform(99) + 1 NSData *data = [NSData dataWithBytes:& random length: sizeof(random)]; int value = *(int*)([data bytes]); How can this be done in Swift? 回答1: Like this: var src: NSInteger = 2525 var out: NSInteger = 0 let data = NSData(bytes: &src, length: sizeof(NSInteger)) data.getBytes(&out, length: sizeof(NSInteger)) println(out) // ==> 2525 回答2: In Swift 4: There are a couple of things to

Decode a base64 data to image

ぐ巨炮叔叔 提交于 2019-11-30 03:58:21
This code doesn't working with Swift 3 anymore. imageData = NSData(base64EncodedString: mediaFile, options: NSDataBase64DecodingOptions.fromRaw(0)!) So is this one. imageData = NSData(base64EncodedString: mediaFile, options: .allZeros) Instead of using NSData use directly Swift 3 native Data . if let decodedData = Data(base64Encoded: mediaFile, options: .ignoreUnknownCharacters) { let image = UIImage(data: decodedData) } Swift 4.1 : Sometimes string has prefix data:image/png;base64 will make base64Encoded return nil , for this situation: extension String { func base64ToImage() -> UIImage? { if

Writing Data to an NSOutputStream in Swift 3

为君一笑 提交于 2019-11-30 03:57:16
The solution of this question no longer works with Swift 3. There is no longer a property bytes of Data (formerly NSData . let data = dataToWrite.first! self.outputStream.write(&data, maxLength: data.count) With this code, I get the error: Cannot convert value of type 'Data' to expected argument type 'UInt8' How can you write Data to an NSOutputStream in Swift 3? NSData had a bytes property to access the bytes. The new Data value type in Swift 3 has a withUnsafeBytes() method instead, which calls a closure with a pointer to the bytes. So this is how you write Data to an NSOutputStream (without

CGPDFDocumentRef from NSData

好久不见. 提交于 2019-11-30 00:32:44
I get my PDF from SQLite DB into a NSData variable. Now what are my options to create CGPDFDocumentRef from this NSData? Or what are my options anyway to create this CGPDFDocumentRef, have the data in SQLite? viggio24 You can create a PDF document using this function: CGPDFDocumentRef CGPDFDocumentCreateWithProvider ( CGDataProviderRef provider ); To create the provider you can use this function: CGDataProviderRef CGDataProviderCreateWithCFData ( CFDataRef data ); and consider that NSData and CFDataRef are toll-free bridged so you can use them interchangeably. So summarizing try this: NSData