Also, that's just a side note, but since you're using MacRuby, I think you 
should try to take advantage of the ruby syntax.
For example:
options = NSMutableDictionary.dictionaryWithObject 
NSNumber.numberWithFloat(0.85), forKey:NSImageCompressionFactor
would become:
options = {NSImageCompressionFactor => 0.85}

I think it makes the code much more readable.

-- 
Thibault Martin-Lagardette



On Aug 11, 2010, at 16:36, Laurent Sansonetti wrote:

> 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 <[email protected]> 
>> 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
>> > [email protected]
>> > http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> [email protected]
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> [email protected]
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> _______________________________________________
> MacRuby-devel mailing list
> [email protected]
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

_______________________________________________
MacRuby-devel mailing list
[email protected]
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

Reply via email to