- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
用数组保存没问题,就是不知道怎么切。我找到了如何将它切成矩形,但找不到如何像比萨饼一样切割它。
@implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)rect {
rect = CGRectMake(rect.origin.x*self.scale,
rect.origin.y*self.scale,
rect.size.width*self.scale,
rect.size.height*self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:self.scale
orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}
@end
在图片上我试图展示我想做什么。
最佳答案
所以核心是你想从图像中剪切 ARC。我不会在这里注释代码,因为我在代码中写了一些注释。
- (NSArray *)pizzaSlicesFromImage:(UIImage *)image withNumberOfSlices:(NSUInteger)numberOfSlices {
if (!image) {
NSLog(@"You need to pass an image to slice it! nil image argument occured."); // use cocoa lumberjack instead of logs
return nil;
} else if (numberOfSlices == 0) { // 0 slices? then you don't want the image at all
return nil;
} else if (numberOfSlices == 1) { // 1 slice? then it's whole image, just wrapped in array
return @[image];
}
CGFloat fullCircle = 2 * M_PI;
CGFloat singleSliceAngle = fullCircle / numberOfSlices;
CGFloat previousSliceStartAngle = 0;
NSMutableArray *mSlicesOfImage = [NSMutableArray new];
for (NSUInteger i = 0; i < numberOfSlices; i++) {
UIImage *sliceImage = [self imageFromAngle:previousSliceStartAngle toAngle:(previousSliceStartAngle + singleSliceAngle) fromImage:image];
if (sliceImage) {
[mSlicesOfImage addObject:sliceImage];
}
previousSliceStartAngle += singleSliceAngle;
}
// return non-mutable array
return mSlicesOfImage.copy;
}
- (UIImage *)imageFromAngle:(CGFloat)fromAngle toAngle:(CGFloat)toAngle fromImage:(UIImage *)image {
// firstly let's get proper size for the canvas
CGRect imageRect = CGRectMake(0.f, 0.f, image.size.width, image.size.height);
CGPoint centerPoint = CGPointMake(CGRectGetMidX(imageRect), CGRectGetMidY(imageRect));
// start the drawing
UIGraphicsBeginImageContext(imageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// we need to perform this to fix upside-down rotation
CGContextTranslateCTM(context, 0, imageRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// now slice an arc from the circle
CGContextBeginPath(context);
CGContextMoveToPoint(context, centerPoint.x, centerPoint.y); // move to the center of drawing
CGContextAddArc(context, centerPoint.x, centerPoint.y, MAX(CGRectGetWidth(imageRect), CGRectGetHeight(imageRect)) / 2.f, fromAngle, toAngle, 0); // draw the arc
// ! if you want NOT to cut outer area to form the circle, just increase 4th value (radius) to cover "corners" of the rect drawn on the circle. I did it this way, because it looks like a pizza, like you wanted.
CGContextClosePath(context);
CGContextClip(context);
// get the image, purge memory
CGContextDrawImage(context, imageRect, image.CGImage);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// this is one slice
return resultImage;
}
这不是一个完整的解决方案,因为我没有更多时间来完成它,但我会尝试稍后完成它。唯一剩下的就是把图像剪小一点,但我没有时间完成它,所以这是一个部分答案。
希望对您有所帮助 :) 希望稍后编辑此答案以添加缺少的剪辑部分。
顺便说一句:我已经基于上述实现创建了一个模块。欢迎您查看:https://github.com/natalia-osa/NORosettaView .
关于ios - 如何将 UIImage 切成碎片(如披萨)并将每个碎片保存在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31855443/
我今天在这里看到了 Ayende 关于 NH 分片的文章 http://ayende.com/blog/4252/nhibernate-shards-progress-report .我第一次听说 N
我正在尝试使用 Scrapy 和 Cloudflare 抓取 URL,但我无法获得任何结果: 2018-07-09 22:14:00 [scrapy.core.engine] INFO: Spider
我用于调试日志的Graylog2服务器上存在一些磁盘问题。现在有未分配的分片: curl -XGET http://host:9200/_cat/shards graylog_292 1 p STAR
我正在做一个小机器人,它应该提供来自网站(ebay)的信息并使用 splinter 和 python 将其放入列表中。我的第一行代码: from splinter import Browser wit
我正在尝试通过 splinter 从模态框的下拉菜单中选择内容。我很容易找到这个下拉菜单,例如: (Pdb) dropdown = next(i for i in my_browser.find_by
我的 APC 总是达到 100% 碎片。我的 VPS 有 1GB 内存,APC 分配给它 256mb,但它只使用了 256mb 中平均 100mb(最大 150mb)的内存。 我必须重新启动 php-
所以我正在使用 python splinter library测试一个网络应用程序,当我检查一个元素是否存在并且我手动找到每个元素来操作它时,我遇到了一个问题。 问题是,当输入列表大于 4 项或更多时
我尝试从具有以下 html 代码的下拉列表中选择“本地主机”: Local Host ah005 这是我的 pyt
我正在使用 Spring 和 Thymeleaf 开发应用程序,我想知道如何使用 thymeleaf 片段。 Thymeleaf 与 JSP 的优点是我们不必运行应用程序来查看模板,但是,当我们将模板
我在 linux 上用 ping 做了一些测试,我有点好奇 DF 位和碎片是如何工作的。我一直在发送一些带有命令 -M do 的包和一些带有 -M dont 的包,我意识到即使发送小于 MTU 的包,
我尝试从原始 H264 视频数据创建片段 MP4,以便我可以在互联网浏览器的播放器中播放它。我的目标是创建实时流媒体系统,媒体服务器会将碎片化的 MP4 片段发送到浏览器。服务器将缓冲来自 Raspb
在 mongodb 中。如果你想构建一个有两个分片的生产系统,每个分片都是一个具有三个节点的副本集,你必须启动多少 mongod 进程? 为什么答案是9? 最佳答案 因为每个分片需要 3 个副本 x
我是 python 和 scrapy 的新手。我正在尝试遵循 Scrapy 教程,但我不明白 storage step 的逻辑. scrapy crawl spidername -o items.js
1、Sharding 的应用场景一般都那些? 当数据库中的数据量越来越大时,不论是读还是写,压力都会变得越来越大。试想,如果一张表中的数据量达到了千万甚至上亿级别的时候,不管是建索引,优化缓存等,
我正在通过以太网发送 2000 字节 JSON(以太网 MTU 1500 字节),因为我的数据包大于以太网 MTU,所以我的消息被分段,如您在下面的 Wireshark 捕获中看到的那样。现在我正在尝
我决定为我的文档实现以下 ID 策略,它将文档“类型”与 ID 结合起来: doc.id = "docType_" + Guid.NewGuid().ToString("n"); // create
是否可以编写 ip6tables 规则来阻止格式错误的 ipv6 分段数据包。这基本上是为了我们电器盒的ipv6认证。我们运行在:rhel 5.5 和内核:2.6.18-238.1.1.el5 我们目
有很多关于 .NET LOH 的可用信息,并且已经在各种文章中进行了解释。但是,似乎有些文章不够精确。 过时信息 在Brian Rasmussen's answer (2009), program m
我还有一个 Unresolved 问题 HERE关于一些可能涉及 LOH 碎片以及其他未知数的绝望内存问题。 我现在的问题是,公认的做事方式是什么?如果我的应用程序需要在 Visual C# 中完成,
经过 20 年的专业发展,我仍然发现自己对数据库性能的某些方面一无所知。这是那些时代之一。这里和其他地方有数以千计的关于表和索引碎片及其对性能影响的问题。我知道基本的注意事项,但有时似乎没有“好的”答
我是一名优秀的程序员,十分优秀!