2012-04-12 79 views
4

我有一個從ALAssetLibrary檢索到的對象ALAsset我想推斷一個壓縮JPEG以便將其發送到Web服務。如何從ALAsset保存壓縮的JPEG

任何建議從哪裏開始?

編輯: 我找到一種方式來獲得的NSData出ALAsset

ALAssetRepresentation *rappresentation = [asset defaultRepresentation]; 
Byte *buffer = (Byte*)malloc(rappresentation.size); 
NSUInteger buffered = [rappresentation getBytes:buffer fromOffset:0.0 length:rappresentation.size error:&err]; 

的,但我不能找到一種方法,通過調整和壓縮它,以減少圖像的大小。 我的想法是有類似:

UIImage *myImage = [UIImage imageWithData:data]; 
//resize image 
NSData *compressedData = UIImageJPEGRepresentation(myImage, 0.5); 

但是,首先,即使沒有調整,只是用代碼compressedData的這兩條線比數據更大。 和第二我不知道什麼是對調整的UIImage

回答

0

可以使用

[theAsset thumbnail] 

還是最好的方式;

壓縮中可能會導致更大的文件之後的某一點,你需要調整圖像大小:

+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation 
0

可以很容易地從一個ALAssetRepresentation得到CGImage:

ALAssetRepresentation repr = [asset defaultRepresentation]; 
// use the asset representation's orientation and scale in order to set the UIImage 
// up correctly 
UIImage *image = [UIImage imageWithCGImage:[repr fullResolutionImage] scale:[repr scale] orientation:[repr orientation]]; 
// then do whatever you want with the UIImage instance