2015-01-04 2 views
0

이 코드를 사용하여 이미지를 스케일링했지만 모든 것이 잘 작동하지만 결과 이미지의 속성에는 투명도가 있고 투명도가없고 알파 채널이없는 이미지가 필요합니다. 전혀 ... 투명 필름이없는 이미지를 얻기 위해 코드를 수정하려면 어떻게해야합니까? 여기 내 코드 :코코아에서 투명도가없는 NSImage를 조절하는 방법

- (void)scalePreviews:(NSString *)outputPath :(NSURL *)nomeImmagine :(CGFloat)size1 :(CGFloat)size2 :(NSString *)nomeIcona 
{ 
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[nomeImmagine path]]; 
if (!image) 
    image = [[NSWorkspace sharedWorkspace] iconForFile:[nomeImmagine path]]; 


NSSize outputSize = NSMakeSize(size1,size2); 
NSImage *anImage = [self scaleImagePreviews:image toSize:outputSize]; 


NSString *finalPath = [outputPath stringByAppendingString:[NSString stringWithFormat:@"/image-previews/%@",nomeIcona]]; 

//questo serve a creare la directory delle icone se gia' non esiste 
NSFileManager *fileManager= [NSFileManager defaultManager]; 
if(![fileManager fileExistsAtPath:[outputPath stringByAppendingString:@"/image-previews"] isDirectory:nil]) 
    if(![fileManager createDirectoryAtPath:[outputPath stringByAppendingString:@"/image-previews"] withIntermediateDirectories:YES attributes:nil error:NULL]) 
     NSLog(@"Error: Create folder failed %@", [outputPath stringByAppendingString:@"/image-previews"]); 

// NSLog(@"finalPath: %@",finalPath); 
NSData *imageData = [anImage TIFFRepresentation]; 

NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData]; 

NSData *dataToWrite = [rep representationUsingType:NSPNGFileType properties:nil]; 

[dataToWrite writeToFile:finalPath atomically:NO]; 


} 





- (NSImage *)scaleImagePreviews:(NSImage *)image toSize:(NSSize)targetSize 
{ 
     if ([image isValid]) 
     { 

      NSSize imageSize = [image size]; 
     float width = imageSize.width; 
     float height = imageSize.height; 
     float targetWidth = targetSize.width; 
     float targetHeight = targetSize.height; 
     float scaleFactor = 0.0; 
     float scaledWidth = targetWidth; 
     float scaledHeight = targetHeight; 

     NSPoint thumbnailPoint = NSZeroPoint; 

     if (!NSEqualSizes(imageSize, targetSize)) 
     { 
      float widthFactor = targetWidth/width; 
      float heightFactor = targetHeight/height; 

      if (widthFactor < heightFactor) 
      { 
       scaleFactor = widthFactor; 
      } 
      else 
      { 
       scaleFactor = heightFactor; 
      } 

      scaledWidth = width * scaleFactor; 
      scaledHeight = height * scaleFactor; 

      newImage = [[NSImage alloc] initWithSize:targetSize]; 

      [newImage lockFocus]; 

      NSRect thumbnailRect; 
      thumbnailRect.origin = thumbnailPoint; 
      thumbnailRect.size.width = targetSize.width; //scaledWidth; 
      thumbnailRect.size.height = targetSize.height; //scaledHeight; 

      [image drawInRect:thumbnailRect 
        fromRect:NSZeroRect 
        operation:NSCompositeSourceOver 
        fraction:1.0]; 

      [newImage unlockFocus]; 
     } 

} 

return newImage; 

} 
+0

"속성의 투명성"이란 무엇을 의미합니까? 원래 이미지가 투명한 곳에 어떤 색을 사용하고 싶습니까? 원본 이미지를 그리기 전에 사각형을 그 색상으로 채우려고 했습니까? 예 : '[[NSColor whiteColor]]]; NSRectFill (thumbnailRect); ' –

+0

코드를 사용해 보았지만 그 문제는 여전히 남아 있습니다 ... "속성의 투명성"이란 이미지 만 허용하는 사이트에서 이미지를 사용할 때 투명성이없는 경우에도 마찬가지입니다 투명도가없고 알파 채널이 없다면 투명도가 있다고 말하면서 오류가 발생합니다 ... 그래서 "DNA"에 있어야한다고 생각합니다 ... 어떻게해야할지 모르겠지만 ... 이미지를 열면 Photoshop을 사용하면 문제가 사라지는 "이미지 병합"명령을 내릴 수 있습니다. – Blue

답변

0

나는 ... 나는 representationUsingType를 변경 ... 이미지가 전혀 투명이 없습니다, 그래서 내가 JPEGFileType를 사용 ...

여기

변경된 코드 솔루션을 발견

NSData * dataToWrite = [rep representationUsingType : NSJPEGFileType properties : nil];

관련 문제