gpt4 book ai didi

ios - 如何使用 objective-c 从 CloudKit 的 Assets 字段中检索图像

转载 作者:行者123 更新时间:2023-11-29 12:05:19 26 4
gpt4 key购买 nike

我在 CloudKit 中有一个记录类型。它存储一个字符串字段和一个 Assets 字段。使用仪表板,我将图像上传到 Assets 字段。

我可以检索字符串字段。我现在需要检索 Assets 字段的内容并使用 objective-c 将其存储到图像中。谁能帮忙?

我正在尝试在 TableView 中显示记录类型。我有一个名为 TJKCategoriesViewController 的类,它继承自 UIViewController。

我在 TJKCategoriesViewController 类的 viewDidLoad 方法中有这段代码:

// get all categories
_jokeCategories = [[NSMutableArray alloc] init];
CKDatabase *jokePublicDatabase = [[CKContainer containerWithIdentifier:JOKE_CONTAINER] publicCloudDatabase];
NSPredicate *predicateCategory = [NSPredicate predicateWithFormat:@"CategoryName != %@", CATEGORY_TO_REMOVE_OTHER];
CKQuery *queryCategory = [[CKQuery alloc] initWithRecordType:CATEGORY_RECORD_TYPE predicate:predicateCategory];
NSSortDescriptor *sortCategory = [[NSSortDescriptor alloc] initWithKey:CATEGORY_FIELD_NAME ascending:YES];
queryCategory.sortDescriptors = [NSArray arrayWithObjects:sortCategory, nil];
[jokePublicDatabase performQuery:queryCategory inZoneWithID:nil completionHandler:^(NSArray<CKRecord*>* results, NSError * error)
{
if (!error)
{
// add all categories to array
for (CKRecord* jokeCategory in results)
{
TJKCategories *categories = [TJKCategories initWithCategory:[jokeCategory valueForKey:CATEGORY_FIELD_NAME] categoryImage:[jokeCategory valueForKey:@"CategoryImage"]];
[_jokeCategories addObject:categories];
}


NSLog(@"Categories = %@",_jokeCategories);

// setup table
[self setupTableContents];
}
else
{
// display alert message and pop the view controller from the stack
GHSAlerts *alert = [[GHSAlerts alloc] initWithViewController:self];
errorActionBlock errorBlock = ^void(UIAlertAction *action) {[self closeCategories:self];};
[alert displayErrorMessage:@"Oops!" errorMessage:@"The joke categories failed to load. This screen will close. Just try again!" errorAction:errorBlock];
}
}];

这是“setupTableContents”方法

// setup the array that will hold the table's contents
-(void)setupTableContents
{
// store to property and reload the table contents
dispatch_async(dispatch_get_main_queue(), ^{
[self.categoriesTableView reloadData];
});
}

TJKCategories 是一个类,用于存储记录类型中的两个字段。这是 TJKCategories.h 文件:

#import <UIKit/UIKit.h>

@interface TJKCategories : NSObject

#pragma Properties

@property (nonatomic, copy) UIImage *categoryImage;
@property (nonatomic, copy) NSString *categoryName;

#pragma Initializers
+(instancetype)initWithCategory:(NSString *)categoryName
categoryImage:(UIImage *)categoryImage;


-(instancetype)initWithCategory:(NSString *)categoryName
categoryImage:(UIImage *)categoryImage;

@end

这是 TJKCategories.m 文件:

// designated initializer
-(instancetype)initWithCategory:(NSString *)categoryName categoryImage:(UIImage *)categoryImage
{
self = [super init];

if (self)
{
self.categoryImage = categoryImage;
self.categoryName = categoryName;
}

return self;
}

// class level initializer
+(instancetype)initWithCategory:(NSString *)categoryName categoryImage:(UIImage *)categoryImage
{
TJKCategories *category = [[self alloc]initWithCategory:categoryName categoryImage:categoryImage];
return category;
}

// override default initializer
-(instancetype)init
{
return [self initWithCategory:@"" categoryImage:nil];
}

在我使用的 viewDidLoad 方法中:[jokePublicDatabase performQuery:queryCategory inZoneWithID:nil completionHandler:^(NSArray* results, NSError * error)

检索记录类型的结果。然后我循环遍历结果数组,我可以检索字符串“CATEGORY_FOR_NAME”(这是“CategoryName”的常量,但是当我尝试检索图像“CategoryImage”的字段时,我得到了“无法识别的选择器发送到实例"错误。

如果你需要知道常量背后的名字是什么,这里是我的联系人文件:

// declare constants
extern NSString *const DEFAULT_CATEGORY;
extern NSString *const JOKE_CONTAINER;
extern NSString *const CATEGORY_RECORD_TYPE;
extern NSString *const CATEGORY_FIELD_NAME;
extern NSString *const CATEGORY_TO_REMOVE_OTHER;
extern NSString *const CATEGORY_TO_REMOVE_RANDOM;
extern NSString *const JOKE_RECORD_TYPE;
extern NSString *const JOKE_DESCR;
extern NSString *const JOKE_SUBMITTED_BY;
extern NSString *const JOKE_TITLE;

// assign values to constants
NSString *const DEFAULT_CATEGORY = @"None";
NSString *const JOKE_CONTAINER = @"iCloud.com.glennseplowitz.Jokes";
NSString *const CATEGORY_RECORD_TYPE = @"Categories";
NSString *const CATEGORY_FIELD_NAME = @"CategoryName";
NSString *const CATEGORY_TO_REMOVE_OTHER = @"Other";
NSString *const CATEGORY_TO_REMOVE_RANDOM = @"Random";
NSString *const JOKE_RECORD_TYPE = @"Jokes";
NSString *const JOKE_DESCR = @"jokeDescr";
NSString *const JOKE_SUBMITTED_BY = @"jokeSubmittedBy";
NSString *const JOKE_TITLE = @"jokeTitle";

所以我要做的是从记录类型中检索 Assets 类型“CategoryImage”并将其存储到 UIImage。但正如我所说,它给我错误“无法识别的选择器发送到实例”,因为我相信它不能将 Assets 字段直接转换为 UIImage 字段。

谢谢,格伦

最佳答案

想通了!我更改了我的 TJKCategories 类以接受 CKAsset 而不是 UIImage。然后我将 CKAsset 转换为 UIImage。

这是 TJKCategories.h 文件:

#import <UIKit/UIKit.h>
#import <CloudKit/CloudKit.h>

@interface TJKCategories : NSObject

#pragma Properties

@property (nonatomic, copy) UIImage *categoryImage;
@property (nonatomic, copy) NSString *categoryName;

#pragma Initializers
+(instancetype)initWithCategory:(NSString *)categoryName
categoryImage:(CKAsset *)categoryImageAsset;


-(instancetype)initWithCategory:(NSString *)categoryName
categoryImage:(CKAsset *)categoryImageAsset;


@end

这是 TJKCategories.m 文件:

#import "TJKCategories.h"

@interface TJKCategories()

#pragma Properities
@property (nonatomic, strong) CKAsset *categoryImageAsset;

@end

@implementation TJKCategories

#pragma initializers

// designated initializer
-(instancetype)initWithCategory:(NSString *)categoryName categoryImage:(CKAsset *)categoryImageAsset
{
self = [super init];

if (self)
{
self.categoryImageAsset = categoryImageAsset;
self.categoryName = categoryName;
UIImage *image = [[UIImage alloc] initWithContentsOfFile:self.categoryImageAsset.fileURL.path];
self.categoryImage = image;
}

return self;
}

// class level initializer
+(instancetype)initWithCategory:(NSString *)categoryName categoryImage:(CKAsset *)categoryImageAsset
{
TJKCategories *category = [[self alloc]initWithCategory:categoryName categoryImage:categoryImageAsset];
return category;
}

// override default initializer
-(instancetype)init
{
return [self initWithCategory:@"" categoryImage:nil];
}

我也改了行:

TJKCategories *categories = [TJKCategories initWithCategory:[jokeCategory valueForKey:CATEGORY_FIELD_NAME] categoryImage:[jokeCategory **objectForKey**:@"CategoryImage"]];

TJKCategories *categories = [TJKCategories initWithCategory:[jokeCategory valueForKey:CATEGORY_FIELD_NAME] categoryImage:[jokeCategory **valueForKey**:@"CategoryImage"]];

请注意,CategoryImage 现在使用的是 valueForKey 而不是 objectForKey。

关于ios - 如何使用 objective-c 从 CloudKit 的 Assets 字段中检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35255503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com