[MacRuby-devel] NSBitmapImageRep representationUsingType

Kenny Lövrin kenny at ustwo.se
Thu Aug 12 01:28:44 PDT 2010


Hey

I have prepared a simple project that contains nothing else but the image downloading/scaling code, but I have kept it so that it works exactly like in our actual project. I have also put two crash logs in the zip, but the date in them isn't todays date so i'm not sure whats going on there to be honest, as it did crash for me just a little while ago.

Here's a link to the file: http://dl.dropbox.com/u/436484/NSBitmapRepTest.zip

I am on another computer at the moment as I'm at work, and it have only crashed once for me on this computer. I still have to test this project on my home computer to see how it acts, but I figured I'll post it here right away anyway. The code for the download and scaling is exactly the same, except that it downloads the same image over and over instead of different ones, and I updated the code with the ruby syntax that Thibault mentioned. It is a bit unpredictable when it will crash, sometimes it works all the way through, others it crashes after 3 images, and then sometimes after 50 and so on. On my imac it does crash 98% of the times though, but on this MBP it has only crashed once so far.

I read somewhere else online that some people had problems using NSBitmapImageRep TIFFRepresentation on any other than the main thread, and that someone suspected it was connected to what type of graphics card was in the computer. I have no idea if this also goes for the method I'm using, ,but I figured it was worth mentioning. I can't find anything in the Apple docs saying it can't be used in background threads, but then again, there's a lot of documentation to search through. ;)

The computer that gives me constant problems is my iMac 24" which I don't remember exactly what gfx card it is on top of my head, but I'm 99% sure it is an ATI card in those. :) The computer I'm running on right now is a Macbook Pro with a NVIDIA GeForce 9400M.

Thanks a lot for the help guys, I really appreciate it, and I love MacRuby. :)

Kenny


12 aug 2010 kl. 01.36 skrev Laurent Sansonetti:

> Hi Kenny,
> 
> Could you try to extract the image scaling code (which seems to cause the crash if I read well) into a smaller program and see if you still have the problem?
> 
> A backtrace would be nice, otherwise. If your app crashes you might have a crash log in ~/Library/Logs/CrashReporter for it.
> 
> Laurent
> 
> On Aug 11, 2010, at 1:43 PM, Kenny Lovrin wrote:
> 
>> Sure, here's the parts that I think is of value:
>> 
>> Heres how my queue is defined:
>> 
>> class MetadataDownloader
>> 
>> 	@@queue = Dispatch::Queue.new("se.kennylovrin.test")
>> 
>> 	class << self
>> 
>> 		def fetch(id)
>> 			downloader = new(id)
>> 			@@queue.async do
>> 				downloader.start
>> 			end
>> 		end
>> 
>> 	end
>> 
>> <more code here.....>
>> end
>> 
>> I use this by looping an array of integers, calling MetadataDownloader.fetch(var), hoping it will instantiate a new downloader and start it in the queue. :)
>> 
>> Then, it does some work, connecting to some API's and downloading som data which contains a url to an image, that url is later passed into this instance method:
>> 
>> def save_poster(url)
>> 		stamp = Time.now.to_i
>> 		save_path = File.join CoreDataManager.instance.app_support_dir, "posters", "#{stamp}.jpg"
>> 		save_path_small = File.join CoreDataManager.instance.app_support_dir, "posters", "#{stamp}_small.jpg"
>> 		
>> 		fm = NSFileManager.defaultManager
>> 		unless fm.fileExistsAtPath save_path
>> 			error = Pointer.new_with_type "@"
>> 			unless fm.createDirectoryAtPath save_path.stringByDeletingLastPathComponent, withIntermediateDirectories:true, attributes:nil, error:error
>> 				NSLog "Failed to create dir for poster"
>> 				return nil
>> 			end
>> 			
>> 			img_data = NSData.dataWithContentsOfURL NSURL.URLWithString(url)
>> 			
>> 			if img_data && fm.createFileAtPath(save_path, contents:img_data, attributes:nil)
>> 				NSLog "Fullsize poster saved"
>> 				jpeg_data = rescaled_image img_data
>> 				unless jpeg_data && jpeg_data.writeToFile(save_path_small, atomically:true)
>> 					NSLog "Failed to scale down poster at #{save_path_small}"
>> 					return nil
>> 				end
>> 				NSLog "Small poster saved"
>> 			else
>> 				NSLog "Failed to save poster at #{save_path}"
>> 				return nil
>> 			end
>> 			
>> 			img_data = nil
>> 		end
>> 		
>> 		save_path_small
>> 	end
>> 
>> which uses this instance method to do the scaling (this seems to be where the problem is):
>> 
>> def rescaled_image(img_data)
>> 		NSLog "Rescaling poster"
>> 		NSLog "Calculating dimensions"
>> 		img = CIImage.imageWithData img_data
>> 		y_scale = 300 / img.extent.size.height
>> 		x_scale = 200 / img.extent.size.width
>> 		scale = [y_scale, x_scale].min
>> 		
>> 		NSLog "Setting up filter"
>> 		scale_filter = CIFilter.filterWithName "CILanczosScaleTransform"
>> 		scale_filter.setValue(NSNumber.numberWithFloat(scale), forKey:"inputScale")
>> 		scale_filter.setValue(NSNumber.numberWithFloat(1.0), forKey:"inputAspectRatio")
>> 		scale_filter.setValue(img, forKey:"inputImage")
>> 		
>> 		NSLog "Getting scaled image from filter"
>> 		img = scale_filter.valueForKey "outputImage"
>> 		
>> 		NSLog "Create bitmap rep from image"
>> 		rep = NSBitmapImageRep.alloc.initWithCIImage img
>> 		NSLog "#{rep}"
>> 		options = NSMutableDictionary.dictionaryWithObject NSNumber.numberWithFloat(0.85), forKey:NSImageCompressionFactor
>> 		options.setValue(NSNumber.numberWithBool(true), forKey:NSImageProgressive)
>> 		
>> 		NSLog "Getting jpeg data from bitmap rep"
>> 		jpeg_data = rep.representationUsingType NSJPEGFileType, properties:options
>> 		
>> 		NSLog "Rescale done, returning data"
>> 		jpeg_data
>> 	end
>> 
>> As I said, it crashes randomly, but always at the line where it's trying t get the jpeg representation.. I'm pretty new to Cocoa so I there might be something stupod going on here.. ;) Everything works as expected if I do not try to do the image scaling..
>> 
>> I think I am on MacRuby rev. 4407, but there is a risk I have done an update to rev 4407 but never built it.. I think that is what I have installed though.
>> 
>> What do I need to do to get a proper crash log? :)
>> 
>> Thanks a lot!
>> Kenny
>> 
>> On 11 August 2010 22:28, Thibault Martin-Lagardette <thibault.ml at gmail.com> wrote:
>> If it's small enough, and if you can, could you probably share the code with us? It might help us debug de problem.
>> If you can't, we may need a little more than just that:
>> - What version of MacRuby are you running?
>> - Can you attach a crashlog?
>> 
>> The more info, the better :-)
>> 
>> --
>> Thibault Martin-Lagardette
>> 
>> 
>> 
>> On Aug 11, 2010, at 12:55, Kenny Lovrin wrote:
>> 
>> > Hey guys
>> >
>> > I have a piece of code that runs in a background thread, and it crashes randomly. I can't see any structure to the crashing, other than it always seem to crash at the following line:
>> >
>> > jpeg_data = rep.representationUsingType NSJPEGFileType, properties:options
>> >
>> > Are there any known issues with this and macruby, or am I wrong somewhere else? I tried running it both on the main thread and also by using sync instead of async when i dispatch the thread to the queue, but it still crashes randomly.
>> >
>> > I'm not entierly sure how to debug this, all I get in the log is an EXC_BAD_ACCESS and then it stops in my editor at objc_msgSend.
>> > Any ideas? :)
>> >
>> > PS. I need to rescale an image, if anyone know any other way that works in a background thread please share. :)
>> >
>> > Thanks!
>> > Kenny
>> > _______________________________________________
>> > MacRuby-devel mailing list
>> > MacRuby-devel at lists.macosforge.org
>> > http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20100812/676fbe4d/attachment-0001.html>


More information about the MacRuby-devel mailing list