iphone - 创建图 block "on the fly"

标签 iphone ios core-graphics tiles

是否有任何解决方案可以“即时”创建性能良好的照片 block ?! 我已经看过here ,但这不是那么高效。

目标是创建一个像 Apple 的照片应用程序一样的快速照片查看器。因此,我需要根据从应用程序创建或下载到应用程序的照片创建图 block 。

最佳答案

最好的选择可能是使用 Core Graphics 和 ImageI/O

CGImageRef image = ...; // the big image to slice, don't draw with this image or it will decompress and might chew up all your memory
NSURL *url = ...; // the url for this slice
NSString *uti = @"public.jpeg"; // just an example, you should get the correct one for your images
CGSize sliceSize = 256.0; // for example
size_t column = 0;
size_t row = 0; // row and column should vary across your image
CGFloat colWidth = 256.0; // should account for the final column not being 256
CGFloat rowWidth = 256.0; // should account for the final row not being 256
CGRect sliceRect = CGRectMake(floor(column * sliceSize), floor(row * sliceSize),
                                    floor(colWidth), floor(rowHeight));
CGImageRef slicedImage = CGImageCreateWithImageInRect(image, sliceRect);
if(NULL == slicedImage) {
    NSLog(@"hosed image");
}
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)url, (CFStringRef)uti, 1, NULL);
CGImageDestinationAddImage(destination, slicedImage, NULL);
if(!CGImageDestinationFinalize(destination)) {
    NSLog(@"hosed write of %@", [url path]);
}
CFRelease(destination);
CGImageRelease(slicedImage);

请注意,这是凭内存拼凑的,可能是错误的。请阅读文档以了解我搞砸的地方。

这里是关于这个技术的文档。

http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGImage/Reference/reference.html#//apple_ref/c/func/CGImageCreateWithImageInRect

http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/ImageIOGuide/imageio_basics/ikpg_basics.html

关于iphone - 创建图 block "on the fly",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5850385/

相关文章:

ios - UISearchBar - UISearchBar 的不同表格 View 背景

swift - 使用 Swift 绘制一个简单的折线图

ios - 获取 NSMutableArray 中相同对象的索引

iphone - 执行 FMDB 插入查询但未在数据库中插入数据

iphone - 防止重新加载缓存的 iPhone Web 应用程序(滚动到顶部)

iphone - 如何删除以前版本的 iPhone 应用程序存储的所有文件?

iphone - 是否需要 "synchronizeFile"来保证 iPhone 上的文件完整性?

ios - swift如何在特定时间设置通知

iphone - 如何设置剪切矩形或区域

ios - 如何将图像添加到 UIImageView?