Reputation: 2676
I have an NSCollectionView displaying images stored internally (ie-not files). The collection is displaying correctly. However, when I start a drag operation, the app crashes. It crashes before the drag ever leaves the collection view. I am returning an internal image for the drag image. The crash log shows that the crash occurs in the drag code. I must be missing something but I cannot find it. My code is based on Apple's sample.
- (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths withEvent:(NSEvent *)event
{
if (indexPaths != nil)
{
NSLog(@"1.0");
return YES;
}
else
return NO;
}
- (id <NSPasteboardWriting>)collectionView:(NSCollectionView *)collectionView pasteboardWriterForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"2");
NSURL *testImageURL;
NSBundle* myBundle = [NSBundle mainBundle];
NSString* myImagePath = [myBundle pathForResource:@"blue-bridge" ofType:@"png"];
testImageURL = [NSURL fileURLWithPath:myImagePath];
//NSURL *anurl = [NSURL URLWithString:@"file:///Users/sysadmin/Downloads/wonderwoman.jpg"] ;
return testImageURL ;
}
pasteboardWriterForItemAtIndexPath is being called and then the app crashes with;
I have tried different images, both internal and external, png and jpeg, so the image itself doesn't seem be the problem.
Suggestions greatly appreciated!
Upvotes: 3
Views: 251
Reputation: 2419
From the stack trace I'd say it's crashing when trying to generate the dragging image. To make sure that's the case implement -collectionView:draggingImageForItemsAtIndexwithEvent:offset: and return any image, such as one of the system ones: [NSImage imageNamed:NSImageNameFolder]
If that doesn't crash then you know for sure the problem is with your NSCollectionViewItem and how it's drawing its UI. Share that code so and we can probably help you.
Upvotes: 3