2014-07-09 2 views
0

이미지를 특정 크기로 압축하는 방법이 있으므로 원하는 NSData를 얻을 수 있지만이 NSData를 UIImage로 변환하고 UIImage를 NSData로 변환하면 NSData 증가 (원래 값에 4 aprox를 곱한 값)의 크기를 확인하십시오.잘못된 컨트롤 파일 크기 NSData에서 얻은 UIImage

UIImage *image = [UIImage imageWithData:imageData]; 
NSData *newData = UIImageJPEGRepresentation(image,1.0f); 

왜 이런 일이 발생합니까? 난 그냥 UIImage 파일 크기 (데이터베이스에 저장)에 대한 제어권을 얻고 싶습니다.

아래 코드를 게시합니다.

미리 감사드립니다.

+ (NSData *) dataOfCompressingImage:(UIImage *)pImage toMaxSize:(CGSize)pSize toFileSizeKB:(NSInteger)pFileSizeKB{ 

CGSize aSize; 
if (pSize.width==0 || pSize.height==0){ 
    aSize = pImage.size; 
}else{ 
    aSize = pSize; 
} 

UIImage *currentImage = [self imageWithImage:pImage scaledToSize:aSize]; 
NSData *imageData = UIImageJPEGRepresentation(currentImage, 1.0f); 
NSInteger currentLength = [imageData length]; 

double factor  = 1.0; 
double adjustment = 1.0/sqrt(2.0); // or use 0.8 or whatever you want 

while (currentLength >= (pFileSizeKB * 1024)){ // 
    factor  *= adjustment; 
    NSLog(@"factor: %f", factor); 

    @autoreleasepool{ 
     imageData = UIImageJPEGRepresentation(currentImage, factor); 
    } 

    if (currentLength == [imageData length]){ 
     break; //Exit While. Reached maximum compression level 
    }else{ 
     currentLength = [imageData length]; 
    } 
} 

return imageData; 
} 

답변

2

당신이 UIImageJPEGRepresentation을 수행 할 때 당신은 JPEG의 주파수 성분을 초래, 1.0의 품질 계수를 사용하고 있기 때문에 그것은 일어나고은 과도하게 높은 해상도로 저장합니다.

이런 식으로 생각해보십시오. 수레 배열을 가지고 있다면 원할 경우 두 배의 배열로 저장할 수 있습니다. 이러한 double은 데이터 유형 크기로 인해 두 배의 메모리를 차지하지만 시작 데이터의 제한된 해상도로 인해 훨씬 ​​더 많은 해상도를 제공하지는 못합니다.

+0

나는 그것을 얻었다, 고마워! – mzurita