UIImage가 UIImageJPEGRepresentation을 여러 번 통과 할 수있는 코드 조각을 디버깅 중이며 버그가 있어야하며 이미지 품질이 악화 될 것이라고 생각했지만 놀라 울 정도로 차이점을 시각적으로 확인하십시오.UIImage를 JPEG 이미지 데이터로 여러 번 압축하기
그래서 테스트를 수행하고 이미지를로드 한 다음 UIImageJPEGRepresentation을 1000 번 통과 시키려고했는데 놀랍게도 1 또는 1000 시간이 이미지 품질을 시각적으로 변화시키지 않았는지 그 이유는 무엇입니까?
이
는 테스트 코드입니다 : UIImage *image = [UIImage imageNamed:@"photo.jpeg"];
// Create a data reference here for the for loop later
// First JPEG compression here
// I would imagine the data here already has low image quality
NSData *data = UIImageJPEGRepresentation(image, 0);
for(int i=0; i<1000; i++)
{
// Convert the data with low image quality to UIImage
UIImage *image = [UIImage imageWithData:data];
// Compress the image into a low quality data again
// at this point i would imagine the image get even more low quality, like u resaved a jpeg twice in phootshop
data = UIImageJPEGRepresentation(image, 0);
}
// up to this point I would imagine the "data" has gone through JPEG compression 1000 times
// like you resave a jpeg as a jpeg in photoshop 1000 times, it should look like a piece of crap
UIImage *imageFinal = [UIImage imageWithData:data];
UIImageView *view = [[UIImageView alloc] initWithImage:imageFinal];
[self.view addSubview:view];
// but it didn't, the final image looks like it has only gone through the jpeg compression once.
편집 : 당신은 오브젝티브 -c에서이 작업을 수행 할 경우 내 의심의 여지가, 간단한 코드로 요약 될 수있다 :
UIImage *image1 = an image..
NSData *data1 = UIImageJPEGRepresentation(image1, 0);
UIImage *image2 = [UIImage imageWithData:data1];
NSData *data2 = UIImageJPEGRepresentation(image2, 0);
UIImage *imageFinal = [UIImage imageWithData:data2];
는 imageFinal 두 번 JPEG 압축을 겪었나요 ?
루프 값을 사용하지 않는 경우 루프의 요점은 무엇입니까? 네가 정말로하고 싶은 일이 혼란 스럽다. 값 0에 대해서는 내부의 다음 단계에서 데이터를 추가하지 않습니다. 더 설명해 주시겠습니까! –
안녕하세요. 제 질문을 수정했습니다. 잘하면 지금은 분명합니다. – mkto
NSLog (@ "% i", data.length)를 시도하는 것은 흥미로울 것입니다. 압축이 한계에 도달했는지 확인하십시오. – Roecrew