Agg*_*sor 10 memory-management uiimage ios swift phasset
我已经彻底阅读了最新的iOS8相框工作,我正在尝试从用户库中获取一些资产来显示.我让用户一次编辑4张图片.但正因为如此,我需要压缩图像,否则应用程序将崩溃.
我使用PHImageManager通过以下代码加载图像:
func processImages()
{
println("Processing")
_selectediImages = Array()
_cacheImageComplete = 0
for asset in _selectedAssets
{
var options:PHImageRequestOptions = PHImageRequestOptions()
options.version = PHImageRequestOptionsVersion.Unadjusted
options.synchronous = true
var minRatio:CGFloat = 1
if(CGFloat(asset.pixelWidth) > UIScreen.mainScreen().bounds.width || CGFloat(asset.pixelHeight) > UIScreen.mainScreen().bounds.height)
{
minRatio = min(UIScreen.mainScreen().bounds.width/(CGFloat(asset.pixelWidth)), (UIScreen.mainScreen().bounds.height/CGFloat(asset.pixelHeight)))
}
var size:CGSize = CGSizeMake((CGFloat(asset.pixelWidth)*minRatio),(CGFloat(asset.pixelHeight)*minRatio))
println("Target size is \(size)")
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize:size, contentMode: .AspectFill, options: options)
{
uiimageResult, info in
var image = iImage(uiimage: uiimageResult)
println("Result Size Is \(uiimageResult.size)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在计算目标大小,以确保图像至少不比屏幕大.如果是,我会在图像上缩小比例.但是这是典型的打印日志
对象尺寸为(768.0,798.453531598513)结果大小是(1614.0,1678.0)
即使我将目标大小设置为768x798(在特定情况下),它给我的最终UIImage也是它的两倍多.现在根据文档中的targetSize参数
"要返回的图像的目标大小."
不是最清楚的解释,但从我的实验来看,它不符合这一点.
如果您有一些建议,我很乐意听到它!
在Swift,你想做这样的事情:
var asset: PHAsset!
var imageSize = CGSize(width: 100, height: 100)
var options = PHImageRequestOptions()
options.resizeMode = PHImageRequestOptionsResizeMode.Exact
options.deliveryMode = PHImageRequestOptionsDeliveryMode.Opportunistic
PHImageManager.defaultManager().requestImage(asset, targetSize: imageSize, contentMode: PHImageContentMode.AspectFill, options: options) {
(image, info) -> Void in
// what you want to do with the image here
print("Result Size Is \(image.size)")
Run Code Online (Sandbox Code Playgroud)
}
在Objective-C,它看起来像这样:
void (^resultHandler)(UIImage *, NSDictionary *) = ^(UIImage *result, NSDictionary *info) {
// what you want to do with the image
};
CGSize cellSize = CGSizeMake(100, 100);
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
[[PHImageManager defaultManager] requestImageForAsset:self.imageAsset targetSize:cellSize contentMode:PHImageContentModeAspectFill options:options resultHandler:resultHandler];
Run Code Online (Sandbox Code Playgroud)
重要说明:使用商机交付模式,结果块可能会以不同的大小调用多次,但最后一次调用将是您想要的大小.最好使用Opportunistic,以便UI首先加载低质量占位符,然后更新它,因为操作系统可以生成更好的图像(而不是有一个空白方块).
这是因为requestImageForAsset将被调用两次.
第一次,它将返回一个非常大小的图像,例如(60*45)我认为是该图像的缩略图.
第二次,您将获得完整尺寸的图像.
我用
if ([[info valueForKey:@"PHImageResultIsDegradedKey"]integerValue]==0){
// Do something with the FULL SIZED image
} else {
// Do something with the regraded image
}
区分两个不同的图像.
参考:iOS 8 PhotoKit.从iCloud照片共享相册中获取最大尺寸的图像
尝试将resizeMode设置为PHImageRequestOptionsResizeModeExactdeliveryMode为PHImageRequestOptionsDeliveryModeHighQualityFormat;
PHImageRequestOptionsResizeModeExact,
// same as above but also guarantees the delivered image is exactly targetSize (must be set when a normalizedCropRect is specified)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11570 次 |
| 最近记录: |