Agg*_*sor 3 performance xcode ios swift phasset
我有一个应用程序,让人们最多可以组合4张图片.然而,当我让他们从他们的照片中选择(最多4张)时,即使我将图像质量设置为FastFormat,它也会非常慢.这将需要4秒(每张照片约1秒).在最高质量,4张图像需要6秒.
无论如何我能建议我更快地获得图像吗?
这是我处理图像的块.
func processImages()
{
_selectediImages = Array()
_cacheImageComplete = 0
for asset in _selectedAssets
{
var options:PHImageRequestOptions = PHImageRequestOptions()
options.synchronous = true
options.deliveryMode = PHImageRequestOptionsDeliveryMode.FastFormat
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize:CGSizeMake(CGFloat(asset.pixelWidth), CGFloat(asset.pixelHeight)), contentMode: .AspectFit, options: options)
{
result, info in
var minRatio:CGFloat = 1
//Reduce file size so take 1/3 the screen w&h
if(CGFloat(asset.pixelWidth) > UIScreen.mainScreen().bounds.width/2 || CGFloat(asset.pixelHeight) > UIScreen.mainScreen().bounds.height/2)
{
minRatio = min((UIScreen.mainScreen().bounds.width/2)/(CGFloat(asset.pixelWidth)), ((UIScreen.mainScreen().bounds.height/2)/CGFloat(asset.pixelHeight)))
}
var size:CGSize = CGSizeMake((CGFloat(asset.pixelWidth)*minRatio),(CGFloat(asset.pixelHeight)*minRatio))
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
result.drawInRect(CGRectMake(0, 0, size.width, size.height))
var final = UIGraphicsGetImageFromCurrentImageContext()
var image = iImage(uiimage: final)
self._selectediImages.append(image)
self._cacheImageComplete!++
println(self._cacheImageComplete)
if(self._cacheImageComplete == self._selectionCount)
{
self._processingImages = false
self.selectionCallback(self._selectediImages)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
不要自己调整图像大小 - 部分原因PHImageManager是为你做这件事.(它还会缓存缩略图,以便下次可以更快地获取它们,并在应用程序之间共享缓存,这样您就不会有六个应用程序最终创建整个库的六个单独的500MB缩略图缓存. )
func processImages() {
_selectediImages = Array()
_cacheImageComplete = 0
for asset in _selectedAssets {
let options = PHImageRequestOptions()
options.deliveryMode = .FastFormat
// request images no bigger than 1/3 the screen width
let maxDimension = UIScreen.mainScreen().bounds.width / 3 * UIScreen.mainScreen().scale
let size = CGSize(width: maxDimension, height: maxDimension)
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: size, contentMode: .AspectFill, options: options)
{ result, info in
// probably some of this code is unnecessary, too,
// but I'm not sure what you're doing here so leaving it alone
self._selectediImages.append(result)
self._cacheImageComplete!++
println(self._cacheImageComplete)
if self._cacheImageComplete == self._selectionCount {
self._processingImages = false
self.selectionCallback(self._selectediImages)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
显着变化:
requestImageForAsset并使用该AspectFill模式.这将为您提供一个图像,无论其纵横比是多少,都可以填充该正方形.FastFormat,所以你可能会得到模糊的图像.)你为什么这么说synchronous?显然这会慢下来.而且,synchronous绝对禁止在主线上说!!!! 阅读文档并遵守它们.这是主要问题.
还有许多其他考虑因素.基本上你使用这个电话都错了.删除后synchronous,请不要像这样处理图像!请记住,这个回调将被多次调用,因为图像以更好和更好的版本提供.你不能在这里做任何耗时的事情.
(另外,为什么要调整图像大小?如果你想要一定大小的图像,你应该在请求它时询问那个大小.让图像获取者为你做的工作.)
| 归档时间: |
|
| 查看次数: |
11151 次 |
| 最近记录: |