문제

iPhone 응용 프로그램에서 사용하는 간단한 MAC 데이터 입력 도구를 구축했습니다. 최근에 간단한 바인딩을 사용하여 이미지를 통해 추가 한 썸네일을 추가했습니다. 잘 작동하는 것으로 보이는 변환 가능한 데이터 유형입니다.

그러나 iPhone 응용 프로그램에는 이미지가 표시되지 않습니다. 속성은 null은 아니지만 이미지가 나타날 수 없습니다. 다음은 CellforyatindexPath입니다

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

NSManagedObject *entity = nil;
if ([self.searchDisplayController isActive])
    entity = [[self filteredListContent] objectAtIndex:[indexPath row]];
else
    entity = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [entity valueForKey:@"name"];
//cell.imageview.image = [UIImage imageNamed:@"ImageB.jpeg"]; //works fine
cell.imageView.image = [entity valueForKey:@"thumbnail"];//no error, but no file

return cell;

문제는 변환 가능 (기본 NSkeyEdunarchiveFromData를 사용하고 있음) 또는 썸네일을 호출하는 방법에 문제가 있다고 생각합니다. 나는 초보자이므로 어떤 도움을 주셔서 감사합니다.

도움이 되었습니까?

해결책

이미지를 데스크탑에서 nsimage로 저장하는 것처럼 들리고 해당 객체가 iPhone에 존재하지 않습니다. 데스크탑 앱은 이미지를 휴대용, PNG 또는 JPG 등으로 저장해야합니다. 그러면 iPhone 응용 프로그램에 UIIMAGE로 다시로드 할 수 있습니다.

RE 변환 가능 업데이트

당신이 여전히 속성에 nsimage를 전달하고있는 것처럼 들리며 데이터를 처리하고 있다고 생각합니다. 다음과 같이 먼저 "표준"형식으로 변환해야합니다.

NSBitmapImageRep *bits = [[myImage representations] objectAtIndex: 0];

NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
[myManagedObject setImage:data];

다음과 같이 사용자 정의 액세서를 작성하는 것이 좋습니다.

#ifdef IPHONEOS_DEPLOYMENT_TARGET

- (void)setImage:(UIImage*)image
{
  [self willChangeValueForKey:@"image"];

  NSData *data = UIImagePNGRepresentation(image);
  [myManagedObject setImage:data];
  [self setPrimitiveValue:data forKey:@"image"];
  [self didChangeValueForKey:@"image"];
}

- (UIImage*)image
{
  [self willAccessValueForKey:@"image"];
  UIImage *image = [UIImage imageWithData:[self primitiveValueForKey:@"image"];
  [self didAccessValueForKey:@"image"];
  return image;
}

#else

- (void)setImage:(NSImage*)image
{
  [self willChangeValueForKey:@"image"];
  NSBitmapImageRep *bits = [[image representations] objectAtIndex: 0];

  NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
  [myManagedObject setImage:data];
  [self setPrimitiveValue:data forKey:@"image"];
  [self didChangeValueForKey:@"image"];
}

- (NSImage*)image
{
  [self willAccessValueForKey:@"image"];
  NSImage *image = [[NSImage alloc] initWithData:[self primitiveValueForKey:@"image"]];
  [self didAccessValueForKey:@"image"];
  return [image autorelease];
}

#endif

이렇게하면 조건부 컴파일이 제공되며 데이터를 모든 장치에서 검색 할 수있는 NSDATA (PNG 형식)로 저장합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top