gpt4 book ai didi

ios - 在图像尺寸缩小的同时显示 HUD

转载 作者:行者123 更新时间:2023-11-29 03:25:41 24 4
gpt4 key购买 nike

我有一个应用程序,用户可以在其中使用相机拍照,然后选择使用照片。调用以下方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

在此方法中,我检查图像的 NSData 长度,如果数据 (Kb) 大小太大,则调整实际图像的大小,然后再次检查。这样我只缩小少量以保持最高质量/尺寸的图像而不是特定尺寸 w/h。

问题我正在尝试在“图像缩放”发生时向用户显示一个 HUD。 HUD暂时不显示,这是我试过的。

// Check if the image size is too large
if ((imageData.length/1024) >= 1024) {
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.labelText = NSLocalizedString(@"HUDHomeLoadingTableData", @"Home View Controller - Loading Table Data");
HUD.removeFromSuperViewOnHide = YES;

while ((imageData.length/1024) >= 1024) {
NSLog(@"While start - The imagedata size is currently: %f KB",roundf((imageData.length/1024)));

// While the imageData is too large scale down the image

// Get the current image size
CGSize currentSize = CGSizeMake(image.size.width, image.size.height);

// Resize the image
image = [image resizedImage:CGSizeMake(roundf(((currentSize.width/100)*80)), roundf(((currentSize.height/100)*80))) interpolationQuality:kMESImageQuality];

// Pass the NSData out again
imageData = UIImageJPEGRepresentation(image, kMESImageQuality);

}

[HUD hide:YES];
}

我正在将 HUD 添加到 self.view 但它没有显示?我是否也应该考虑在这里使用线程,图像缩放是否应该在后台线程上完成并且 HUD 在主线程上更新。我不确定何时确定某些部分是否应该在不同的线程上?

最佳答案

您只调整一张图像的大小吗?还是几个?如果这样做,您可以:

MBProgressHUD *HUD = [self showHUD];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// do your image resizing here

// when done, hide the HUD on the main queue

dispatch_async(dispatch_get_main_queue(), ^{
[self hideHUD:HUD];
});
});

哪里

- (MBProgressHUD *)showHUD
{
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.labelText = NSLocalizedString(@"HUDHomeLoadingTableData", @"Home View Controller - Loading Table Data");
HUD.removeFromSuperViewOnHide = YES;

return HUD;
}

- (void)hideHUD:(MBProgressHUD *)HUD
{
[HUD hide:YES];
}

如果调整一堆图像的大小,您应该定义自己的队列来调整大小:

MBProgressHUD *HUD = [self showHUD];

NSOperationQueue *resizeQueue = [[NSOperationQueue alloc] init];
resizeQueue.maxConcurrentOperationCount = 1;

NSOperation *completeOperation = [NSBlockOperation blockOperationWithBlock:^{

//when done, hide the HUD (using the main queue)

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self hideHUD:HUD];
}];
}];

for (NSInteger i = 0; i < imageCount; i++)
{
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
// do your image resizing here
}];

// make the completion operation dependent upon each image resize operation

[completionOperation addDependency:operation];

// queue the resize operation

[resizeQueue addOperation:operation];
}

// when all done, queue the operation that will remove the HUD

[resizeQueue addOperation:completionOperation];

请注意,我假设您一次(连续)执行一个操作,但如果您想同时执行这些操作,只需将 maxConcurrentOperationCount 调整为您想要的任何值即可。坦率地说,考虑到这一切的重点是您希望使巨大的图像变小,您可能不想同时运行太多图像(因为内存使用问题),但这是一种选择。

关于ios - 在图像尺寸缩小的同时显示 HUD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20483242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com