NSImageView > NSDraggingSession

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • NSImageView > NSDraggingSession

    Hallo,

    laut Dokumentation ist -dragImage:… depreacted und es soll -beginDraggingSessionWithItems:… benutzt werden.

    Ich möchte folgendes realisieren:

    - Anwender zieht Bild vom NSImageView
    - das Bild soll am Mauszeiger hängen
    - bewegt er das Bild z.B. in ein Finder-Fenster, dann soll am Bild ein + erscheinen
    - lässt er es los, dann soll es dahin "exportiert" werden

    Als solches ist das kein Problem und ich habe das schon oft gebaut.
    Nur mit NSDraggingSession blicke ich nicht ganz durch.

    Kann mir das jemand stichwortartig zusammenfassen?

    Viele Grüße
  • Wie, weiß es niemand?

    Ich habe es jetzt mal so gelöst:

    1.)
    -mouseDown: ruft bei mir -beginDraggingSessionWithEvent: auf

    2.)
    NSPasteboardItemDataProvider prüft auf kPasteboardTypeFileURLPromise und speichert dann die Datei in dem Ordner, der sich aus NSPasteboard auslesen lässt

    Viele Grüße

    C-Quellcode

    1. -(BOOL)beginDraggingSessionWithEvent:(NSEvent*)event
    2. {
    3. // …
    4. NSImage *image = [self image];
    5. if(image == nil)
    6. {
    7. return NO;
    8. }
    9. // …
    10. NSArray *array = [self draggingTypes];
    11. if([array count] < 1)
    12. {
    13. return NO;
    14. }
    15. // …
    16. NSPasteboardItem *pasteboardItem = [[[NSPasteboardItem alloc] init] autorelease];
    17. if(pasteboardItem == nil)
    18. {
    19. return NO;
    20. }
    21. // …
    22. [pasteboardItem setDataProvider:self
    23. forTypes:array];
    24. // …
    25. NSDraggingItem *draggingItem = [[[NSDraggingItem alloc] initWithPasteboardWriter:pasteboardItem] autorelease];
    26. if(draggingItem == nil)
    27. {
    28. return NO;
    29. }
    30. // …
    31. NSRect rect = [self bounds];
    32. // …
    33. [draggingItem setDraggingFrame:rect
    34. contents:image];
    35. // …
    36. array = [NSArray arrayWithObject:draggingItem];
    37. if([array count] < 1)
    38. {
    39. return NO;
    40. }
    41. // …
    42. NSDraggingSession *draggingSession = [self beginDraggingSessionWithItems:array
    43. event:event
    44. source:self];
    45. if(draggingSession == nil)
    46. {
    47. return NO;
    48. }
    49. // …
    50. return YES;
    51. // …
    52. }
    Alles anzeigen

    C-Quellcode

    1. -(NSString*)ti_copyPasteLocation
    2. {
    3. // …
    4. NSPasteboard *pasteboard = self;
    5. if(pasteboard == nil)
    6. {
    7. return nil;
    8. }
    9. // …
    10. NSString *string = [pasteboard name];
    11. if([string length] < 1)
    12. {
    13. return nil;
    14. }
    15. // …
    16. PasteboardRef pasteboardRef = NULL;
    17. PasteboardCreate((CFStringRef)string, &pasteboardRef);
    18. if(pasteboardRef == NULL)
    19. {
    20. return nil;
    21. }
    22. // …
    23. PasteboardSynchronize(pasteboardRef);
    24. // …
    25. CFURLRef URLRef = NULL;
    26. PasteboardCopyPasteLocation(pasteboardRef, &URLRef);
    27. if(URLRef == NULL)
    28. {
    29. // …
    30. CFRelease(pasteboardRef);
    31. // …
    32. return nil;
    33. // …
    34. }
    35. // …
    36. string = [(NSURL*)URLRef path];
    37. if([string length] < 1)
    38. {
    39. // …
    40. }
    41. // …
    42. CFRelease(pasteboardRef);
    43. CFRelease(URLRef);
    44. // …
    45. return string;
    46. // …
    47. }
    Alles anzeigen

    C-Quellcode

    1. -(void)pasteboard:(NSPasteboard *)pasteboard
    2. item:(NSPasteboardItem *)item
    3. provideDataForType:(NSString *)type
    4. {
    5. // …
    6. if([type length] < 1)
    7. {
    8. return;
    9. }
    10. // …
    11. if([type isEqualToString:(NSString*)kPasteboardTypeFileURLPromise] == YES)
    12. {
    13. // …
    14. NSString *string = [pasteboard ti_copyPasteLocation];
    15. if([string length] < 1)
    16. {
    17. return;
    18. }
    19. // …
    20. string = [self saveImageInDirectory:string];
    21. if([string length] < 1)
    22. {
    23. return;
    24. }
    25. // …
    26. NSURL *URL = [NSURL fileURLWithPath:string];
    27. if(URL == nil)
    28. {
    29. return;
    30. }
    31. // …
    32. string = [URL absoluteString];
    33. if([string length] < 1)
    34. {
    35. return;
    36. }
    37. // …
    38. [item setString:string
    39. forType:type];
    40. // …
    41. }
    42. if([type isEqualToString:NSPasteboardTypePNG] == YES)
    43. {
    44. // …
    45. NSData *data = [self imageDataForType:NSPNGFileType];
    46. if([data length] < 1)
    47. {
    48. return;
    49. }
    50. // …
    51. [pasteboard setData:data
    52. forType:type];
    53. // …
    54. }
    55. // …
    56. }
    Alles anzeigen