사용자 도구

사이트 도구


사이드바

카테고리

language:cocoa:drawing-guide
정리하다가 그만 둔 페이지

내용 정리 진행 중.. / 원문

Images

이미지 처리는 OSX 어플리케이션에서 중요한 부분. 중요하면서 여러가지의 역할을 수행하게 되는데, 기존의 컨텐츠(Preexist content)를 렌더링하거나 그리기 기능을 수행하는 버퍼 기능을 할 수 있음. 이미지처리의 핵심 부분은 NSImage 클래스이며 다음과 같은 역할을 수행하게 됨

  • 디스크상에서 이미지를 로딩하는 기능
  • 뷰에 이미지를 그리는 기능
  • 새로운 이미지를 만드는 기능
  • 이미지의 확대/축소, 크기조정
  • 이미지 포맷의 변경

이미지 파일을 로딩해서 ( JPEG, GIF, PDF, and EPS 과 같은파일들을 ) 인터페이스상에 그려 넣을 수 있는데, 이런 작업은 쉬운편임 ( 또는 만족스럽지 않을 수도 )

또한 이미지를 오프스크린(Offscreen)버퍼나 임시버퍼로 사용해서 그리는 과정을 (렌더링을) 수행해서 마지막 한장의 이미지를 만드는데 사용할 수도 있음.

비트맵이 이미지를 다루는 일반적인 방법이지만, NSImage가 사진이나 비트맵 데이터만을 다룬다고 생각해서는 안됨. NSImage 는 여러가지 포맷의 이미지를 출력할 수 있으며, 사진이나 비트맵데이터를 표준에 부합하는 여러 포맷을 지원함.(?) 또한 벡터, 복합적인 데이터 포맷 ( PDF, EPS, PICT 와 같은 편집을 위한 파일 포맷 )을 처리할 수 있는 기능을 제공함. Core Image 프레임워크에서 생성된 이미지도 NSImage클래스에서 사용이 가능함. (← 엉망이네..)

여기서는:

  • 이미지처리의 기본
  • 지원하는 이미지 파일 포맷
  • 이미지를 사용하는 가이드라인
  • NSImage 오브젝트 만들기
  • 이미지 작업
  • 이미지 대응 클래스 작업

이미지처리의 기본

NSImage 클래스는 여러가 포맷의 이미지를 처리할 수 있는 하이레벨 인터페이스를 갖고 있음. 하지만 NSImage 클래스가 하이레벨 인터페이스를 제공하기 때문에 직접 이미지 데이터를 처리할 수 있는 방법을 갖고 있진 않음. 대신 NSImageRep로부터 상속되는 이미지 데이터에 대응되는 이미지 리프리젠테이션 클래스를 갖고 있음.

이미지 리프리젠테이션 클래스는 포맷에 맞는 이미지 데이터에 대한 것을 알고 있으며, 현재 컨텍스트에 이미지 데이터를 렌더링 할 수 있다.

다음 섹션 부분은 이미지 오브젝트와 이미지 리프젠테이션의 관계에 대해서 좀 더 자세히 다룬다.

Image Representations

이미지 리프리젠테이션은 NSImage 오브젝트가 이미지 데이터를 사용하기 위해서 쓰게 된다.

이미지의 사이즈, 컬러스페이스, 데이터포맷에 대한 부분을 대응하게 된다.

파일에서 이미지 데이터를 읽고, 쓰는 방법을 알고 있으며, 이미지 데이터를 좀 더 raw bitmap 으로 변환하여 컨텍스트에 이미지를 렌더링할 수 있다. 특정 이미지 리프리젠테이션의 경우 이미지데이터를 직접 다룰 수도 있다.

파일의 이미지의 경우, NSImage는 한 파일에 분리해서 저장할 목적으로 이미지 리프리젠테이션 오브젝트를 생성할 수 있다. 보통은 이미지 하나당 한개의 이미지만을 저장하지만 TIFF 의 경우에는 여러 장의 이미지를 저장 할 수 있다. 예를 들어 TIFF는 썸네일과 원래 이미지를 둘다 저장할 수 있다.

이미지를 동적으로 생성하는 경우, 필요한 이미지 리프리젠테이션을 직접 만들어줘야 한다. 파일의 이미지의 경우에는 한개의 이미지 리프리젠테이션만을 만들면 된다. NSImage가 스케일링(캔바스크기에 맞게 조절하기)이 가능하기에 다른 해상도의 이미지 리프리젠테이션을 만들 필요는 없다. 어쨋든 다음의 경우에는 여러장의 이미지 리프리젠테이션이 필요할 수도 있다.

프린트를 하는 경우, PDF 리프리젠테이션이나 고해상도의 비트맵이 필요하게 될 수도 있다.
You want to provide different content for your image when it is scaled to different sizes.
이미지를 출력하는 경우, NSImage 오브젝트는 타켓 캔바스에 맞는 최선의 리프리젠테이션을 찾게 된다. 이 선택은 몇가지 조건에 맞게 선택하게 되는데 자세한 것은 “How an Image Representation Is Chosen.” 내용을 봐야 한다.
특정 이미지 리프리젠테이션이 정확하게 사용되게끔 하려면 NSImage 클래스의 drawRepresentation:inRect: 메소드를 사용하면 된다.

Image Representation Classes

모든 이미지리프리젠테이션은 NSImageRep 클래스의 서브클래스임 (반복학습으로 바보만드나?)
코코아에는 자주 사용되는 포맷에 대해서 이 서브클래스를 만들어 두었음. 표 6-1은 좀 더 상세한 부분에 대해서 다루고 잇다.

  • 표는 웹을 참조

참고 내용

  • 일반적인 이미지 파일 포맷의 경우 : NSBitmapImageRep
  • 오프스크린용 : NSCachedImageRep
  • PDF를 위한 : NSPDFImageRep

How an Image Representation Is Chosen

When you tell an NSImage object to draw itself, it searches its list of image representations for the one that best matches the attributes of the destination device. In determining which image representation to choose, it follows a set of ordered rules that compare the color space, image resolution, bit depth, and image size to the corresponding values in the destination device. The rules are applied in the following order:

Choose an image representation whose color space most closely matches the color space of the device. If the device is color, choose a color image representation. If the device is monochrome, choose a gray-scale image representation. Choose an image representation whose resolution (pixels per inch) matches the resolution of the device. If no image representation matches exactly, choose the one with the highest resolution. By default, any image representation with a resolution that’s an integer multiple of the device resolution is considered a match. If more than one representation matches, NSImage chooses the one that’s closest to the device resolution. You can force resolution matches to be exact by passing NO to the setMatchesOnMultipleResolution: method.

This rule prefers TIFF and bitmap representations, which have a defined resolution, over EPS representations, which do not. You can use the setUsesEPSOnResolutionMismatch: method to cause NSImage to choose an EPS representation in case a resolution match is not possible.

Choose a representation whose bit depth (bits per sample) matches the depth of the device. If no representation matches, choose the one with the highest number of bits per sample. You can change the order in which these rules are applied using the methods of NSImage. For example, if you want to invert the first and second rules, pass NO to the setPrefersColorMatch: method. Doing so causes NSImage to match the resolution before the color space.

If these rules fail to narrow the choice to a single representation—for example, if the NSImage object has two color TIFF representations with the same resolution and depth—the chosen representation is operating-system dependent.

Images and Caching

NSImage 클래스는 드로잉 성능을 위해서 캐슁 스키마(정책?)을 따르게 된다. 캐슁스키마는 중요하기 때문에 모든 이미지오브젝트에 대해서 기본으로 설정되게 된다. 어쨋든 옵션을 바꾸고 싶다면 NSImage의 setCacheMode: 에서 바꿀 수 있다.
6-2는 사용가능한 캐슁모드에 대해서 보여주고 있다.

  • 웹페이지 참조

Caching and Image Data Retention

Because caching can lead to multiple copies of the image data in memory, NSImage usually dismisses the original image data once a cached copy is created. Dismissing the original image data saves memory and improves performance and is appropriate in situations where you do not plan on changing the image size or attributes. If you do plan on changing the image size or attributes, you may want to disable this behavior. Enabling data retention prevents image degradation by basing changes on the original image data, as opposed to the currently cached copy.

To retain image data for a specific image, you must send a setDataRetained: message to the NSImage object. Preferably, you should send this message immediately after creating the image object. If you send the message after rendering the image or locking focus on it, Cocoa may need to read the image data more than once.

Guidelines for Using Images

Here are some guidelines to help you work with images more effectively:

  • Use the NSImage interface whenever possible. NSImage의 목적은 이미지데이터를 쉽게 쓸 수 있도록 해주는 것. 필요시에는 이미지 리프리젠테이션을 사용하도록한다. NSImage와 연관된 리프리젠테이션은 불변의 오브젝트로 간주해야 한다. NSImage의 다른 목적은 타켓캔바스에 이미지를 효율적으로 출력하게 하는 것. 이미지 리프리젠테이션에서 이미지 데이터를 직접 다루는 것은 피해야한다 (?)
    Avoid manipulating the data of an image representation directly, especially if there are alternatives to manipulating the data, such as compositing the image and some other content into a new image object.
  • 스크린에 그리는 경우, 내장된 캐슁방법을 사용하는것이 가장 나음. 다른 방법을 써보고 싶다면 NSBitmapImageRep 클래스보다는 NSCachedImageRep가 더 나을 것. 캐쉬된 이미지 리프리젠테이션을 CGImageRef 오브젝트로 저장한다면 Quartz에 의해 비디오카드에 저장될 수 있다. (뭘까? 이 안드로메다틱한 설명은)
  • There is little benefit to storing multiple representations of the same image (possibly at different sizes) in a single NSImage. Modern hardware is powerful enough to resize and scale images quickly. The only reason to consider storing multiple representations is if each of those representations contains a customized version of the image.
  • If caching is enabled and you modify an image representation object directly, be sure to invoke the recache method of the owning NSImage object. Cocoa relies on cached content wherever possible to improve performance and does not automatically recreate its caches when you modify image representations. You must tell the image object to recreate its caches explicitly.
  • Avoid recreating art that is already provided by the system. Mac OS X makes several standard pieces of artwork available for inclusion in your own interfaces. This artwork ranges from standard icons to other elements you can integrate into your controls. You load standard images using the imageNamed: method. For a list of standard artwork, see the constants section in NSImage Class Reference.

Mac OS X defines several technologies for working with images. Although the NSImage class is a good general purpose class for creating, manipulating, and drawing images, there may be times when it might be easier or more efficient to use other imaging technologies. For example, rather than manually dissolving from one image to another by drawing partially transparent versions of each image over time, it would be more efficient to use Core Image to perform the dissolve operation for you. For information about other image technologies, and when you might use them, see “Choosing the Right Imaging Technology.”

Creating NSImage Objects

NSString* imageName = [[NSBundle mainBundle] pathForResource:@"image1" ofType:@"JPG"];
NSImage* tempImage = [[NSImage alloc] initWithContentsOfFile:imageName];

Loading an Existing Image

Loading a Named Image

Drawing to an Image

NSImage* anImage = [[NSImage alloc] initWithSize:NSMakeSize(100.0,  100.0)];
[anImage lockFocus];
 
// Do your drawing here...
 
[anImage unlockFocus];
 
// Draw the image in the current context.
[anImage drawAtPoint:NSMakePoint(0.0, 0.0)
            fromRect: NSMakeRect(0.0, 0.0, 100.0, 100.0)
            operation: NSCompositeSourceOver
            fraction: 1.0];

Creating a Bitmap

language/cocoa/drawing-guide.txt · 마지막으로 수정됨: 2022/12/07 22:30 저자 kieuns