从 NSMutableArray 检索时 UIImage 不显示
请帮助:)
我已经设置了一个可变数组,如下所示
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不太可能将图像存储为
UIImage
对象。在存储之前,您必须将其转换为NSData
对象。在将其设置为UIImageView
实例之前,您必须使用initWithData:
将其转换回UIImage
对象。It is unlikely that you are storing the image as a
UIImage
object. You must be converting it into aNSData
object before storing it. You must convert it back to aUIImage
object usinginitWithData:
prior to setting it to theUIImageView
instance.