Nsstring 如何将图像和密钥存储到NSMutableDictionary

Nsstring 如何将图像和密钥存储到NSMutableDictionary,nsstring,uiimage,nsmutabledictionary,Nsstring,Uiimage,Nsmutabledictionary,我在向NSMutableDictionary“dictionary”写入时收到错误 从“我的项目列表”类实现(以下)时: - (void)setImage:(UIImage *)i forKey:(NSString *)s { [dictionary setObject:i forKey:s]; // Create full path for image NSString *imagePath = [self imagePathForKey:s]; // Turn image into JPE

我在向NSMutableDictionary“dictionary”写入时收到错误

从“我的项目列表”类实现(以下)时:

- (void)setImage:(UIImage *)i forKey:(NSString *)s
{
[dictionary setObject:i forKey:s];
// Create full path for image
NSString *imagePath = [self imagePathForKey:s];

// Turn image into JPEG data,
NSData *d = UIImageJPEGRepresentation(i, 0.5);

// Write it to full path
[d writeToFile:imagePath atomically:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *oldKey = [_detailItem imageKey];

//did the item already have an image
if (oldKey) {
    //delete old image
    [[RoomList sharedStore] deleteImageForKey:oldKey];
}

//get picked image from info dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

//create CFUUID object; it knows how to create uique identifier strings
CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);

//create string from unique identifier
CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);

//use unique ID to set our item's imageKey
NSString *key = (__bridge NSString *)newUniqueIDString;
[_detailItem setImageKey:key];
NSLog(@"image key: %@", key);

[_detailItem setImage:image forKey:key];

//tell objects ointed to by newUniqueIDString and newUniqueID to lose an owner to avoid
//memory leak
CFRelease(newUniqueIDString);
CFRelease(newUniqueID);

//remove image picker (dismiss method)
[self dismissViewControllerAnimated:YES completion:nil];
[self configureView];
}
来自DetailViewController:imagePickerController:

- (void)setImage:(UIImage *)i forKey:(NSString *)s
{
[dictionary setObject:i forKey:s];
// Create full path for image
NSString *imagePath = [self imagePathForKey:s];

// Turn image into JPEG data,
NSData *d = UIImageJPEGRepresentation(i, 0.5);

// Write it to full path
[d writeToFile:imagePath atomically:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *oldKey = [_detailItem imageKey];

//did the item already have an image
if (oldKey) {
    //delete old image
    [[RoomList sharedStore] deleteImageForKey:oldKey];
}

//get picked image from info dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

//create CFUUID object; it knows how to create uique identifier strings
CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);

//create string from unique identifier
CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);

//use unique ID to set our item's imageKey
NSString *key = (__bridge NSString *)newUniqueIDString;
[_detailItem setImageKey:key];
NSLog(@"image key: %@", key);

[_detailItem setImage:image forKey:key];

//tell objects ointed to by newUniqueIDString and newUniqueID to lose an owner to avoid
//memory leak
CFRelease(newUniqueIDString);
CFRelease(newUniqueID);

//remove image picker (dismiss method)
[self dismissViewControllerAnimated:YES completion:nil];
[self configureView];
}
我遇到以下错误,该错误与[\u detailItem setImage:image-forKey:key]有关;imagePickerController中的操作:-[NSManagedObject setImage:forKey:]:未识别的选择器发送到实例0x8195770

我的猜测是UIImage*image、NSString*键或两者都不能传递给objectForKey方法“setImage:forKey”。有没有办法解决这个问题,或者绕开它


谢谢

\u detailItem是NSManagedObject,它无法识别setImage forKey选择器。您必须对此进行验证,以确保设置了正确的选择器,可能是setValue:forKey:如果您将托管对象基础设置为NSData或coredata中的id 100%正确,谢谢!“setImage:forKey”不是NSManagedObject的一部分。将其更改为适当的属性,一切正常。