[Rubycocoa-devel 714] Problems with Threads and CoreImage

Back to archive index

Benjamin Jackson bhjac****@gmail*****
Sun Jan 28 03:20:27 JST 2007


This code snippet runs fine in the main thread, but my app crashes
when I put it in a thread as below. Any idea what I'm doing wrong?
Thanks,

Ben

Thread.start do
  rows.times do |row|
    puts "\nRow #{row}"
    columns.times do |column|
      write_column image, row, column
    end
  end
end

def write_column image, row, column
  puts "Column #{column}"
  dest = "#{@dirname}/TileGroup0/#{@numtiers}-#{row}-#{column}.jpg"
  image_width, image_height = image.extent.size.width, image.extent.size.height
  tile_width, tile_height = TILESIZE, TILESIZE
  tile_width = image_width - row*TILESIZE if row*TILESIZE + TILESIZE >
image_width
  tile_height = image_height - column*TILESIZE if column*TILESIZE +
TILESIZE > image_height
  write_image image, dest, {
    :w => row*TILESIZE, :y => column*TILESIZE, :width => tile_width,
:height => tile_height
  }
  @numtiles = @numtiles + 1
end

def write_image image, path, options={}
  x, y = 0, image.extent.size.height
  width = options[:width] || image.extent.size.width
  height = options[:height] || image.extent.size.height
  x = options[:x] unless options[:x].nil?
  # we have to calculate the y from the top to conform to Flash's
coordinate system
  y = image.extent.size.height - options[:y] - height unless options[:y].nil?

  @mutex.synchronize do
    bitmap = OSX::NSBitmapImageRep.alloc.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(
    nil, width, height, 8, 4, true, false, OSX::NSDeviceRGBColorSpace, 0, 0)

    context = OSX::NSGraphicsContext.graphicsContextWithBitmapImageRep bitmap
    OSX::NSGraphicsContext.saveGraphicsState
    OSX::NSGraphicsContext.setCurrentContext context
    context.CIContext.drawImage_atPoint_fromRect image,
OSX::CGPoint.new(0, 0), OSX::CGRect.new(OSX::CGPoint.new(x,y),
OSX::CGSize.new(width, height))
    OSX::NSGraphicsContext.restoreGraphicsState
    representation =
bitmap.representationUsingType_properties(OSX::NSJPEGFileType, nil)
    representation.writeToFile_atomically(path, true)
  end
end



More information about the Rubycocoa-devel mailing list
Back to archive index