私のアプリでは、ユーザーが画像を選択するか、UIImagePickerViewControllerを使って画像を撮ります。画像を選択したら、そのサムネイルを正方形のUIImageView(90x90)に表示したいと思います。UIImagePickerViewControllerの四角いサムネイル画像
私はApple's codeを使用してサムネイルを作成しています。問題は、サムネイルが二乗されていないことです。関数は、kCGImageSourceThumbnailMaxPixelSizeキーを90に設定した後、画像の高さのサイズを変更するように見え、kCGImageSourceThumbnailMaxPixelSizeキーはサムネイルの高さと幅を設定する必要があります。ここで
は私のコードを垣間見るです:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation (image, 0.5);
// My image view is 90x90
UIImage *thumbImage = MyCreateThumbnailImageFromData(imageData, 90);
[myImageView setImage:thumbImage];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
UIImage* MyCreateThumbnailImageFromData (NSData * data, int imageSize) {
CGImageRef myThumbnailImage = NULL;
CGImageSourceRef myImageSource;
CFDictionaryRef myOptions = NULL;
CFStringRef myKeys[3];
CFTypeRef myValues[3];
CFNumberRef thumbnailSize;
// Create an image source from NSData; no options.
myImageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data,
NULL);
// Make sure the image source exists before continuing.
if (myImageSource == NULL){
fprintf(stderr, "Image source is NULL.");
return NULL;
}
// Package the integer as a CFNumber object. Using CFTypes allows you
// to more easily create the options dictionary later.
thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &imageSize);
// Set up the thumbnail options.
myKeys[0] = kCGImageSourceCreateThumbnailWithTransform;
myValues[0] = (CFTypeRef)kCFBooleanTrue;
myKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
myValues[1] = (CFTypeRef)kCFBooleanTrue;
myKeys[2] = kCGImageSourceThumbnailMaxPixelSize;
myValues[2] = thumbnailSize;
myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
(const void **) myValues, 2,
&kCFTypeDictionaryKeyCallBacks,
& kCFTypeDictionaryValueCallBacks);
// Create the thumbnail image using the specified options.
myThumbnailImage = CGImageSourceCreateThumbnailAtIndex(myImageSource,
0,
myOptions);
UIImage* scaled = [UIImage imageWithCGImage:myThumbnailImage];
// Release the options dictionary and the image source
// when you no longer need them.
CFRelease(thumbnailSize);
CFRelease(myOptions);
CFRelease(myImageSource);
// Make sure the thumbnail image exists before continuing.
if (myThumbnailImage == NULL) {
fprintf(stderr, "Thumbnail image not created from image source.");
return NULL;
}
return scaled;
}
そして、私のイメージ図がインスタンス化されるか、この:
myImageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect rect = imageView.frame;
rect.size.height = 90;
rect.size.width = 90;
imageView.frame = rect;
[imageView setUserInteractionEnabled:YES];
私はimageView.contentMode = UIViewContentModeScaleAspectFit;
サムネイルはそれ以来、歪むことになる設定されていない場合私の元の画像の高さが90ピクセルのちょうどバージョンです。
なぜ、サムネイルが二乗されないのですか?ここで
注 - のiOSの知識を持つメタユーザーは、いくつかの超便利なコードを離れて編集しました以下のCleverの答えから。実際にプログラミングして実際に役に立つ答えが得られ、実際にClever(壮大な)アイデアを使用したい場合は、以下の「編集」ボタンをクリックするだけで履歴が表示され、切り取り、貼り付け、アプリストアに送信できます: ) – Fattie
@JoeBlow Clever Errorの回答の[編集履歴](http://stackoverflow.com/posts/17884863/revisions)に削除されたコードはありません。 – cpburnz
http://stackoverflow.com/posts/17884863/revisions rev "5" – Fattie