Hãy thử điều này trên cho kích thước:
Tiêu đề:
@interface UIImage (ImageDivider)
- (UIImage*)imageWithDividerAt:(CGFloat)position width:(CGFloat)width color:(UIColor*)color;
- (UIImage*)imageWithDividerAt:(CGFloat)position patternImage:(UIImage*)patternImage;
- (NSArray*)imagesBySlicingAt:(CGFloat)position;
@end
Thực hiện:
@implementation UIImage (ImageDivider)
- (UIImage*)imageWithDividerAt:(CGFloat)position patternImage:(UIImage*)patternImage
{
//pattern image
UIColor *patternColor = [UIColor colorWithPatternImage:patternImage];
CGFloat width = patternImage.size.width;
//set up context
UIGraphicsBeginImageContext(self.size);
CGContextRef context = UIGraphicsGetCurrentContext();
//draw the existing image into the context
[self drawAtPoint:CGPointZero];
//set the fill color from the pattern image color
CGContextSetFillColorWithColor(context, patternColor.CGColor);
//this is your divider's area
CGRect dividerRect = CGRectMake(position - (width/2.0f), 0, width, self.size.height);
//the joy of image color patterns being based on 0,0 origin! must set phase
CGContextSetPatternPhase(context, CGSizeMake(dividerRect.origin.x, 0));
//fill the divider rect with the repeating pattern from the image
CGContextFillRect(context, dividerRect);
//get your new image and viola!
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage*)imageWithDividerAt:(CGFloat)position width:(CGFloat)width color:(UIColor *)color
{
//set up context
UIGraphicsBeginImageContext(self.size);
CGContextRef context = UIGraphicsGetCurrentContext();
//draw the existing image into the context
[self drawAtPoint:CGPointZero];
//set the fill color for your divider
CGContextSetFillColorWithColor(context, color.CGColor);
//this is your divider's area
CGRect dividerRect = CGRectMake(position - (width/2.0f), 0, width, self.size.height);
//fill the divider's rect with the provided color
CGContextFillRect(context, dividerRect);
//get your new image and viola!
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (NSArray*)imagesBySlicingAt:(CGFloat)position
{
NSMutableArray *slices = [NSMutableArray array];
//first image
{
//context!
UIGraphicsBeginImageContext(CGSizeMake(position, self.size.height));
//draw the existing image into the context
[self drawAtPoint:CGPointZero];
//get your new image and viola!
[slices addObject:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
}
//second
{
//context!
UIGraphicsBeginImageContext(CGSizeMake(self.size.width - position, self.size.height));
//draw the existing image into the context
[self drawAtPoint:CGPointMake(-position, 0)];
//get your new image and viola!
[slices addObject:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
}
return slices;
}
Khái niệm này rất đơn giản - bạn muốn một hình ảnh với chia rút ra qua nó. Bạn chỉ có thể che phủ một chế độ xem hoặc ghi đè drawRect:
hoặc bất kỳ số lượng giải pháp nào. Tôi muốn cung cấp cho bạn loại này. Nó chỉ sử dụng một số cuộc gọi Core Graphics nhanh để tạo ra một hình ảnh với bộ chia mong muốn của bạn, có thể là hình mẫu hoặc màu sắc, ở vị trí đã chỉ định. Nếu bạn muốn hỗ trợ cho ngăn ngang là tốt, nó là khá tầm thường để sửa đổi này như vậy. Tiền thưởng: Bạn có thể sử dụng hình ảnh xếp kề làm dải phân cách!
Bây giờ, hãy trả lời câu hỏi chính của bạn. Sử dụng danh mục khá tự giải thích - chỉ cần gọi một trong hai phương pháp trên nền nguồn của bạn để tạo một phương thức có dải phân cách và sau đó áp dụng hình ảnh đó thay vì hình ảnh nguồn gốc.
Bây giờ, câu hỏi thứ hai rất đơn giản - khi bộ chia đã được di chuyển, hãy tạo lại hình ảnh dựa trên vị trí bộ chia mới. Đây thực sự là một cách thực hiện tương đối kém hiệu quả, nhưng điều này phải đủ nhẹ cho mục đích của bạn cũng như chỉ là một vấn đề khi di chuyển dải phân cách. Tối ưu hóa sớm cũng giống như một tội lỗi.
Câu hỏi thứ ba cũng đơn giản - gọi imagesBySlicingAt:
- nó sẽ trả về một mảng gồm hai hình ảnh, được tạo bằng cách cắt qua hình ảnh ở vị trí được cung cấp. Sử dụng chúng như bạn muốn.
Mã này đã được thử nghiệm để hoạt động. Tôi đặc biệt khuyên bạn nên chơi đùa với nó, không vì mục đích tiện ích nào, nhưng để hiểu rõ hơn về cơ chế được sử dụng để lần sau, bạn có thể ở bên trả lời của mọi thứ
Cảm ơn câu trả lời của bạn nhưng tôi đã sử dụng mã này và nó hoạt động tốt. Làm thế nào tôi có thể thêm đường màu đỏ trên hình ảnh và sử dụng như thế nào tôi có thể chia hình ảnh. bất kỳ tham chiếu hoặc ý tưởng nào –
https://github.com/bilalmughal/NLImageCropper. Điều này có thể giúp bạn. Nhưng bạn phải làm việc trên nó để quản lý dây của bạn. – Shardul
Tôi đã thử BFCropInterface và BJImageCropper nhưng điều này cả hai cắt hình ảnh không chia hình ảnh –