Làm cách nào để chia nhỏ UIImage Trong nửa (xuống giữa) để nó có thể tạo ra hai hình ảnh?Chia UIImage làm đôi?
Trả lời
Bạn có thể thử này,
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
CGImageRef tmpImgRef = image.CGImage;
CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width, image.size.height/2.0));
UIImage *topImage = [UIImage imageWithCGImage:topImgRef];
CGImageRelease(topImgRef);
CGImageRef bottomImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, image.size.height/2.0, image.size.width, image.size.height/2.0));
UIImage *bottomImage = [UIImage imageWithCGImage:bottomImgRef];
CGImageRelease(bottomImgRef);
hy vọng điều này có thể giúp bạn, :)
Trả lời từ câu hỏi Stackoverflow khác. Source
UIImage *splitMeInTwo = [UIImage imageNamed:@"Star.png"];
UIImageView *original = [[UIImageView alloc] initWithImage:splitMeInTwo];
[self.view addSubview:original];
[original release];
// The size of each part is half the height of the whole image
CGSize size = CGSizeMake([splitMeInTwo size].width, [splitMeInTwo size].height/2);
// Create image-based graphics context for top half
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
// Draw into context, bottom half is cropped off
//[image drawAtPoint:CGPointMake(0.0,0.0)];
[splitMeInTwo drawAtPoint:CGPointMake(0.0,0.0) blendMode:kCGBlendModeNormal alpha:1.0];
// Grab the current contents of the context as a UIImage
// and add it to our array
UIImage *top = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *topV = [[UIImageView alloc] initWithImage:top];
CGRect frameTop = topV.frame;
frameTop.origin.x += 360.0f;
topV.frame = frameTop;
[self.view addSubview:topV];
[topV release];
UIGraphicsEndImageContext();
- (void)splitImage:(UIImage *)image
{
CGFloat imgWidth = image.size.width/2;
CGFloat imgheight = image.size.height;
CGRect leftImgFrame = CGRectMake(0, 0, imgWidth, imgheight);
CGRect rightImgFrame = CGRectMake(imgWidth, 0, imgWidth, imgheight);
CGImageRef left = CGImageCreateWithImageInRect(image.CGImage, leftImgFrame);
CGImageRef right = CGImageCreateWithImageInRect(image.CGImage, rightImgFrame);
// These are the images we want!
UIImage *leftImage = [UIImage imageWithCGImage:left];
UIImage *rightImage = [UIImage imageWithCGImage:right];
// Don't forget to free the memory!
CGImageRelease(left);
CGImageRelease(right);
}
Đây là câu trả lời duy nhất không tạo ra toàn bộ các ngữ cảnh. Quartz làm một công việc tuyệt vời (lấy dữ liệu pixel của ảnh ngay lập tức, thay vì tạo một bối cảnh mới, vẽ hình ảnh, sau đó cắt) để tối ưu hóa quá trình cắt ảnh. Điều này có khả năng nhanh hơn nhiều lần khi chúng tôi chia nhỏ hình ảnh thành nhiều phần nhỏ! –
iOS 11, nhanh chóng cập nhật phiên bản 4.0 https://stackoverflow.com/users/893872/durul-dalkanat mà tôi nghĩ phải trung thực là tốt nhất :)
Điều này chia hình ảnh thành 4 phần
func splitImage(image2D: UIImage) -> [UIImage] {
let imgWidth = image2D.size.width/2
let imgHeight = image2D.size.height/2
var imgImages:[UIImage] = []
let leftHigh = CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight)
let rightHigh = CGRect(x: imgWidth, y: 0, width: imgHeight, height: imgHeight)
let leftLow = CGRect(x: 0, y: imgHeight, width: imgWidth, height: imgHeight)
let rightLow = CGRect(x: imgWidth, y: imgHeight, width: imgWidth, height: imgHeight)
let leftQH = image2D.cgImage?.cropping(to:leftHigh)
let rightHQ = image2D.cgImage?.cropping(to:rightHigh)
let leftQL = image2D.cgImage?.cropping(to:leftLow)
let rightQL = image2D.cgImage?.cropping(to:rightLow)
imgImages.append(UIImage(cgImage: leftQH!))
imgImages.append(UIImage(cgImage: rightHQ!))
imgImages.append(UIImage(cgImage: leftQL!))
imgImages.append(UIImage(cgImage: rightQL!))
return imgImages
}
- 1. c + + chia chuỗi bằng đường đôi mới
- 2. Cách xem khác biệt của dự án github chia đôi
- 3. npm cài đặt git chia đôi với submodule
- 4. Phân chia hiệu quả gấp đôi theo công suất 2
- 5. mất bất ngờ độ chính xác khi chia đôi
- 6. hành vi Odd khi chia đôi trong Java
- 7. Đứa trẻ cuối cùng được chia đôi sẽ không chết
- 8. Thêm UIImage lên trên một UIImage khác
- 9. Xoay UIImage?
- 10. Làm cách nào để thay đổi hướng màn hình chia đôi mặc định?
- 11. Làm cách nào để cập nhật repo được chia đôi bằng SourceTree?
- 12. Làm thế nào tôi có thể chia hai số nguyên để có được gấp đôi?
- 13. iPhone SDK - Cách vẽ UIImage lên UIImage khác?
- 14. Sự khác nhau giữa [UIImage imageNamed ...] và [UIImage imageWithData ...]?
- 15. Điều chỉnh UIImage thành UIImageView
- 16. iPhone: Blur UIImage
- 17. đeo mặt nạ UIImage
- 18. So sánh UIImage
- 19. Cắt UIImage thành alpha
- 20. Thêm UIImage vào CCSprite?
- 21. thêm UIImage vào CALayer
- 22. UIImage Không Transparent
- 23. Dữ liệu meta UIImage
- 24. Tạo UIImage từ CIImage
- 25. Tạo UiImage từ NSData
- 26. UIImage: điền vào nền
- 27. iPhone - UIImage Leak, CGBitmapContextCreateImage Leak
- 28. ASIHTTP: Tải lên UIImage?
- 29. Đặt đường viền UIImage
- 30. Gửi UIImage qua NSOutputStream
Điều này hiệu quả. Cảm ơn! – Shredder2794