Trong ứng dụng của tôi, người dùng chọn một hình ảnh hoặc chụp ảnh bằng UIImagePickerViewController. Khi hình ảnh đã được chọn, tôi muốn hiển thị hình thu nhỏ của nó trên một hình vuông UIImageView (90x90).Hình thu nhỏ hình vuông từ hình ảnh UIImagePickerViewController
Tôi đang sử dụng Apple's code để tạo hình thu nhỏ. Vấn đề là hình thu nhỏ không được bình phương, chức năng, sau khi đặt khóa kCGImageSourceThumbnailMaxPixelSize thành 90, dường như chỉ thay đổi kích thước chiều cao của hình ảnh và theo như tôi biết khóa kCGImageSourceThumbnailMaxPixelSize sẽ chịu trách nhiệm thiết lập chiều cao và chiều rộng của hình thu nhỏ.
Đây là một cái nhìn thoáng qua của mã của tôi:
- (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;
}
Và điều này như thế nào xem hình ảnh của tôi được khởi tạo:
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];
Nếu tôi không đặt imageView.contentMode = UIViewContentModeScaleAspectFit;
hình thu nhỏ sẽ bị bóp méo, vì nó chỉ là một phiên bản hình ảnh gốc của tôi với chiều cao là 90 pixel.
Vì vậy, tại sao hình thu nhỏ của tôi không được bình phương?
Note - một người sử dụng Meta không có kiến thức của iOS sửa đi một số mã siêu tiện dụng từ câu trả lời của Clever dưới đây. nếu bạn thực sự lập trình và thực sự muốn có câu trả lời hữu ích và thực sự muốn sử dụng ý tưởng của Clever, bạn chỉ cần nhấp vào nút "đã chỉnh sửa" bên dưới, bạn sẽ thấy lịch sử và có thể cắt, dán và gửi tới cửa hàng ứng dụng:) – Fattie
@JoeBlow Tôi không thấy bất kỳ mã bị xóa nào trong [edit history] (http://stackoverflow.com/posts/17884863/revisions) của câu trả lời của Clever Error. – cpburnz
http://stackoverflow.com/posts/17884863/revisions rev "5" – Fattie