从 NSMutableArray 检索时 UIImage 不显示

发布于 2024-11-11 05:12:02 字数 2176 浏览 3 评论 0原文

请帮助:)

我已经设置了一个可变数组,如下所示

- (void)viewDidLoad {
[super viewDidLoad];


NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TopImage"  inManagedObjectContext:managedObjectContext];
[request setEntity:entity];


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"topImage" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];


NSError *error = nil;
NSMutableArray *mutableFetchResultsT = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResultsT == nil) {
}

[self setTopImageArray:mutableFetchResultsT];}

,我尝试使用以下方式填充 UIImageView:

- (void)showPhoto:(NSInteger)numberToAdd
{

[topIV setImage:[topImageArray objectAtIndex:currentPI + numberToAdd]];

}

currentPI 是一个整数,在索引中标记当前图像,并在此处设置要添加的数字:

- (IBAction)nextTouch
{
[self showPhoto:-1];

}

这将跳到下一个图像(感谢@XenElement)为了这个见解)。

UIImageView 中根本没有加载任何图像。

MutableArray 通过 imagePickerController 进行填充。

以下是我专用的 Image>Data Data>Image 转换器类

@implementation Image2Data

+ (BOOL)allowsReverseTransformation {
return YES;
}

+ (Class)transformedValueClass {
return [NSData class];
}

- (id)transformedValue:(id)value {
return UIImagePNGRepresentation(value);
}

- (id)reverseTransformedValue:(id)value {
return [[[UIImage alloc] initWithData:value] autorelease];
}

@end

这是在我的托管对象类中初始化的:

+ (void)initialize {
if (self == [Images class]) {
    Image2Data *transformer = [[Image2Data alloc] init];
    [NSValueTransformer setValueTransformer:transformer forName:@"Image2Data"];
}
 }

提前感谢您的任何建议

DetartrateD

我最初提出的:

[topImageArray insertObject:imageS atIndex:0];

更正为:

[topImageArray insertObject:imageS.topImage atIndex:0];

:))))大笑,我指着对我的实体来说不是它的属性啊好吧,吸取教训!

Help please :)

I have set up a mutable array as follows

- (void)viewDidLoad {
[super viewDidLoad];


NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TopImage"  inManagedObjectContext:managedObjectContext];
[request setEntity:entity];


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"topImage" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];


NSError *error = nil;
NSMutableArray *mutableFetchResultsT = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResultsT == nil) {
}

[self setTopImageArray:mutableFetchResultsT];}

And im trying to populate a UIImageView using :

- (void)showPhoto:(NSInteger)numberToAdd
{

[topIV setImage:[topImageArray objectAtIndex:currentPI + numberToAdd]];

}

currentPI is an integer marking current image in index and number to add is set here:

- (IBAction)nextTouch
{
[self showPhoto:-1];

}

This will skip to next image (thankyou to @XenElement for this insight).

No images load in UIImageView at all.

MutableArray is getting populated through imagePickerController.

The following is my dedicated Image>Data Data>Image converter class

@implementation Image2Data

+ (BOOL)allowsReverseTransformation {
return YES;
}

+ (Class)transformedValueClass {
return [NSData class];
}

- (id)transformedValue:(id)value {
return UIImagePNGRepresentation(value);
}

- (id)reverseTransformedValue:(id)value {
return [[[UIImage alloc] initWithData:value] autorelease];
}

@end

This is initialized in my managed objects class with:

+ (void)initialize {
if (self == [Images class]) {
    Image2Data *transformer = [[Image2Data alloc] init];
    [NSValueTransformer setValueTransformer:transformer forName:@"Image2Data"];
}
 }

Thank you in advance for any suggestions

DetartrateD

I had originally put:

[topImageArray insertObject:imageS atIndex:0];

corrected to:

[topImageArray insertObject:imageS.topImage atIndex:0];

:)))) big laugh, I was pointing to my entity not its attribute ahh well, lesson learned!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2024-11-18 05:12:02

您不太可能将图像存储为 UIImage 对象。在存储之前,您必须将其转换为 NSData 对象。在将其设置为 UIImageView 实例之前,您必须使用 initWithData: 将其转换回 UIImage 对象。

It is unlikely that you are storing the image as a UIImage object. You must be converting it into a NSData object before storing it. You must convert it back to a UIImage object using initWithData: prior to setting it to the UIImageView instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文