Frage

I am able to add NSImages to my NSCollectionView without having to first save them on disk. The images are fed into the collection view from an NSMutableArray. This way people can see the images without first having to save them.

Is there something similar that I can achieve with IKImageBrowserView? NSCollectionView is functional when it comes to representing images, but I would like to see if I can do something similar with IKImageBrowserView.

I can easily implement IKImageBrowserView with images saved on disk (Apple docs cover how this works) but can't figure out exactly where to look or how to go about adding images to the browser view directly from NSMutableArray instead of first saving them images to disk.

I'm at a loss here. Apart from the docs, I'm not really sure where else to look for direction. Or what to even call what I'm looking to do.

EDIT: (Here's some of the code)

// The data object -- if it is possible to represent an image object, this is where I am probably going wrong.

@interface ImageObject : NSObject

@property (readwrite, copy) NSImage *image;
@property (readwrite, copy) NSString *imageID;

- (id)initWithImage:(NSImage *)anImage;

- (NSString *)imageUID;
- (NSString *)imageRepresentationType;
- (id)imageRepresentation;

@end

@implementation ImageObject

@synthesize image = _image;
@synthesize imageID = _imageID;

- (id)initWithImage:(NSImage *)anImage
{
    if (self = [super init]) {
        _image = [anImage copy];
    }
    return self;
}

- (NSString *)imageUID
{
    return _imageID;
}

- (NSString *)imageRepresentationType
{
    return IKImageBrowserNSImageRepresentationType;
}

- (id)imageRepresentation
{
    return _image;
}

@end

// This is how objects are supposed to be added to the browserView. All of this is straight from Apple.

- (void)updateDatasource
{
    [_browserImages addObjectsFromArray:_importedImages];
    [_importedImages removeAllObjects];
    [imageBrowser reloadData];
}

- (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView *)aBrowser
{
    return [_browserImages count];
}

- (id)imageBrowser:(IKImageBrowserView *)aBrowser itemAtIndex:(NSUInteger)index
{
    return [_browserImages objectAtIndex:index];
}

This is where I try to add NSImages to the browserView but nothing happens. The array gets populated (which means the images are generated without any errors) but nothing happens on the screen.

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[oPanel URL] options:nil];

NSMutableArray *timesArray = [self generateTimeForSpecifiedNumberOfFramesInVideo:10 UsingAsset:asset];

self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];

[[self imageGenerator] generateCGImagesAsynchronouslyForTimes:timesArray completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {

   NSImage *testImage = [[NSImage alloc] initWithCGImage:image size:NSZeroSize];

   if (result == AVAssetImageGeneratorSucceeded) {
       ImageObject *objects = [[ImageObject alloc] initWithImage:testImage];
       [_importedImages addObject:objects];
   }
}

As for exploring the rest of the search results...been there done that. If I did miss anything, kindly mark this question as duplicate indicating what post already existed where this issue has been addressed.

EDIT:

I have accepted the answer below. Along with the unique IDs problem. I had overlooked a simple thing which was the requirement to call the updateDatasource method.

War es hilfreich?

Lösung

The most important point of using IKImageBrowser is create a unique image ID for each element. The following is an example. In fact, it comes from the project that I'm currently working on. I have just implemented IKImageBrowser in it. The code below assumes that you have 36 images (Image01.png, Image02.png..., Image36.png) imported into the project.

// .h
@interface AppDelegate : NSObject {
    IBOutlet IKImageBrowserView *browserView;
    NSMutableArray *imageArray;
}

// .m
#import "IKBBrowserItem.h"
#import <QuartzCore/QuartzCore.h>

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    imageArray = [[NSMutableArray alloc]init];
}

- (void)populateImage {    
    for (NSInteger i2 = 1; i2 <= 36 ; i2++) {
        NSString *name;
        if (i2 < 10) {
            name = [NSString stringWithFormat:@"Image0%ld",(long)i2];
        } else {
            name = [NSString stringWithFormat:@"Image%ld",(long)i2];
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        NSImage *Image0 = [NSImage imageNamed:name];
        NSInteger ran = [self genRandom:1000000:9999999];
        NSString *imageID = [NSString stringWithFormat:@"%@%li",name,ran];
        IKBBrowserItem *item = [[IKBBrowserItem alloc] initWithImage:Image0 imageID:imageID:name];
        [imageArray addObject:item];
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }

    [browserView reloadData];
}

- (NSInteger)genRandom: (NSInteger)min :(NSInteger)max {
    int num1;
    do {
        num1 = arc4random() % max;
    } while (num1 < min);
    return num1;
}

You don't need to use a random integer generator (genRandom) above, but just make sure that no imageID is the same.

This web site has a sample project, which should get you going. (I have no affiliation.) So make sure you download and run it. Then take a closer look and improve it for your needs.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top